×

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 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.

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.

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.

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 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 getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute 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 If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.

Callback and Callback Hell in JavaScript

The widely used client-side programming language JavaScript is simple, light, and interpretive. JavaScript is used by the majority of client-side web applications. It is also possible to use JavaScript on...

3 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.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute 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.

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 redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 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 Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.