Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findIndex() javascript array object

 var array = [{"one":1, "two":2},{"one":3, "two":4}];

            var result = array.findIndex(function (value) {
                if (value === 2) {
                    return false;
                }
                return true;
            });

            console.log(result); 

i keep getting '0' in the console. how should i change (value ===2) ? i have tried change to (value === {"two":2}) but still return '0'.

is there any other array method that suitable ?

like image 798
Zachary Lordford Avatar asked Mar 26 '26 19:03

Zachary Lordford


1 Answers

You need to check one of the properties of the objects of the array. Then return the result of the check.

var array = [{ one: 1, two: 2 }, { one: 3, two: 4 }],
    result = array.findIndex(function(object) {
        return object.two === 2;
    });

console.log(result);
like image 78
Nina Scholz Avatar answered Mar 28 '26 10:03

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!