How To Use Array Find Method In JavaScript With Example

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

In Java Script we can find specified value in an array using find() method .The find() method returns the value of the first element in an array that pass a test (provided as a function).today we are going to discuss how to use find method in javascript

How To Use Array Find Method In JavaScript With Example

The find() method returns the value of the first element in an array that pass a test (provided as a function).

The find() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)

  • Otherwise it returns undefined

     

Syntax and Usage

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


Parameter Values

function(currentValue, index, arr)  Required. A function to be run for each element in the array.

                                    Function arguments:
                                    Argument   Description
                                    currentValue   Required. The value of the current element
                                    index  Optional. The array index of the current element

arr                                    Optional. The array object the current element belongs to

thisValue                          Optional. A value to be passed to the function to be used as its "this" value.
                                    If this parameter is empty, the value "undefined" will be passed as its "this" value


Let's start

Example(1)

1.Create array elements

var Numbers = [3, 10, 18, 20];


2.Now implement code to find data which is equal to 18

function check_given_number(num) {
    return num >= 3;
}

function myFunction() {
    document.getElementById("demo").innerHTML = Numbers.find(check_given_number);
}

Example(2)
<script>
    var ages = [4, 12, 16, 20];

    function checkAdult(age) {
        return age >= document.getElementById("ageToCheck").value;
    }

    function myFunction() {
        document.getElementById("demo").innerHTML = ages.find(checkAdult);
    }
</script>


In above Example Get the value of the first element in the array that has a value above a specific number


Complete Code Of Array Find Method In JavaScript With Example

<!DOCTYPE html>
<html>
<head>
    <title>How To Use Array Find Method In JavaScript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    body{
        color:white;
        background: black;
    }
</style>
<body>
<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>Array Find Method In JavaScript</h1>
    </div>
    <br>
        <button class="btn btn-primary" onclick="myFunction()">Click it</button>
        <h1 id="demo"></h1>
    <br>
</div>
</body>
</html>

<script>
    var Numbers = [3, 10, 18, 20];

    function check_given_number(num) {
        return num >= 3;
    }

    function myFunction() {
        document.getElementById("demo").innerHTML = Numbers.find(check_given_number);
    }
</script>

 

Related Post