×

Javascript Find Object In Array

What is find() method?

It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able to satisfy the testing function, then “undefined” is returned by the find() method.
The find() method does not execute the function on empty arrays, i.e, the array with no elements.
The execution of this method does not modify the original array.
This method is supported by mostly all the browsers like Google chrome 45, safari 7.1, Microsoft Edge 12, Opera 32, Firefox 25 etc. However, it does not provide support to Internet Explorer.

The find() method can take various parameters such as:

  • Callback Function

It refers to the function that executes each value in the array. It usually takes three arguments, that are:

  • Element

It means the current element of the array. It is a compulsory argument.

  • Index

It refers to the position(index) of the current element of the array. It is an optional argument.

  • Array

It refers to the array on which the find() method was called on. It is an optional argument.

  • This value

It means the value that can be passed to the function to be used as its "this" value.
However, if this parameter is empty, the value "undefined" is passed as its "this" value. It is an optional argument.

Find() method and callback function

For every index of the array, the find method executes the “Callback function” atleast once, until the “callback function” returns the “true” value.

  • If it happens, the find() method immediately returns the value of that element and skips checking the remaining values.
  • Else if it returns “false”, the find() method returns undefined.

Syntax:

array.find(function(currentValue, index, arr),thisValue)

Example 1:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript Find object in array
    </title>
</head>




<body>


<h2>JavaScript find() method in arrays</h2>


<p>The example show how the find() method returns the value of the first element in an array that passes the condition, which is provided in the a function.</p>


<p id="tcontent"></p>


<script>
const values = [3, -10, 18, -6,-9,20];


document.getElementById("tcontent").innerHTML = values.find(checkPos);


function checkPos(values) {
  return values > 0;
}
</script>


<p>Here, the find() method is searching for positive numbers in the array, and returns the first positive number it encounters.</p>


</body>
</html>

Output:

Javascript Find Object In Array

Example 2:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript Find object in array
    </title>
</head>
<body>


<h2>JavaScript find() method in arrays</h2>




<p>The example show how the find() method returns the value of the first element in an array that passes the condition, which is provided in the a function.</p>






<p><input type="number" id="CheckPosNum" value="0"></p>


<button onclick="myFunctiondemo()">Click here</button>


<p id="tcontent"></p>


<script>
const values = [3, -10, 18, -6,-9,20];


function checkPos(values) {
  return values > document.getElementById("CheckPosNum").value;
}


function myFunctiondemo() {
  document.getElementById("tcontent").innerHTML = values.find(checkPos);
}
</script>




<p>Here, the find() method is searching for positive numbers in the array, and returns the first positive number it encounters.</p>
</body>
</html>

Output:

Javascript Find Object In Array

After clicking on the button, the function check the value of elements in the array with “value” provided in the input tag(specified by the user), and displays the first element fulfilling the condition,

Javascript Find Object In Array

NOTE: The value in the <input> tag can be incremented or decremented by the user accordingly, through the arrow buttons, as shown below:

Javascript Find Object In Array

Related Topics

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.

Javascript Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

Synchronous and Asynchronous in JavaScript

JavaScript is a powerful and versatile language that is used all over the web, from small websites to large applications. It is essential for developers to understand the differences between...

9 minutes read.

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 minutes read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript String

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript: String literal. Using new keyword. String literal By using double quotes...

4 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

Javascript Cookies

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of...

1 minute read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

Javascript Tutorial

JavaScript is a light-weight, dynamic scripting language that is mainly employed to create interactive front-end web applications. It was originally developed in 1995 by Brendan Eich at Netscape Communications Corporation...

5 minutes read.

Javascript Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

9 minutes read.