findIndex()JavaScript数组对象

findIndex()JavaScript数组对象

问题描述:

 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); 

我一直在控制台中获得"0".我应该如何更改(值=== 2)?我已经尝试更改为(value === {"two":2})但仍返回"0".

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 ?

您需要检查数组对象的属性之一.然后返回检查结果.

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);