findIndex()JavaScript数组对象 [英] findIndex() javascript array object

查看:46
本文介绍了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);

这篇关于findIndex()JavaScript数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆