Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用下划线.js查找数组中对象的索引?_Javascript_Angularjs_Underscore.js_Javascript Framework - Fatal编程技术网

Javascript 如何使用下划线.js查找数组中对象的索引?

Javascript 如何使用下划线.js查找数组中对象的索引?,javascript,angularjs,underscore.js,javascript-framework,Javascript,Angularjs,Underscore.js,Javascript Framework,我想使用下划线.js获取数组中给定值的索引 这是我的箱子 var array = [{'id': 1, 'name': 'xxx'}, {'id': 2, 'name': 'yyy'}, {'id': 3, 'name': 'zzz'}]; var searchValue = {'id': 1, 'name': 'xxx'}; 我使用了以下代码 var index = _.indexOf(array, function(data) {

我想使用下划线.js获取数组中给定值的索引

这是我的箱子

var array = [{'id': 1, 'name': 'xxx'},
             {'id': 2, 'name': 'yyy'},
             {'id': 3, 'name': 'zzz'}];

var searchValue = {'id': 1, 'name': 'xxx'};
我使用了以下代码

var index = _.indexOf(array, function(data) { 
                alert(data.toSource()); //For testing purpose 
                return data === searchValue; 
            });
我也试过这个

var index = _.indexOf(array, {id: searchValue.id});
但是它
返回-1
。因为它没有进入那个功能。所以我没有收到警告信息

我的代码有什么问题。
有人能帮我吗

对于对象,
==
=
检查两个引用是否引用了相同的对象;它不检查等效对象:

您必须测试对象的属性才能做出决定。在您的情况下,
id
属性看起来是一个不错的选择,或者如果您想比较所有属性,可以使用下划线。

使用以下选项:

var array = [{'id': 1, 'name': 'xxx'},
             {'id': 2, 'name': 'yyy'},
             {'id': 3, 'name': 'zzz'}];

var searchValue = {'id': 1, 'name': 'xxx'},
    index = -1;

_.each(array, function(data, idx) { 
   if (_.isEqual(data, searchValue)) {
      index = idx;
      return;
   }
});

console.log(index); //0
在代码段
data===searchValue
中比较对象的引用,您不想这样做。另一方面,如果您使用
data==searchValue
您将比较对象的字符串表示,即
[Object Object]
如果您没有重新定义
toString
方法


因此,比较对象的正确方法是使用
。isEqual

可能是我的建议,我会给你一些建议

为什么对indexof方法使用回调?Underline.js中indexof的签名如下:

_.indexOf(array, value, [isSorted]) 
“查找”可能更适合此任务:

_.find(array, function(item, index) {
  if (item.id == searchValue.id) {
    alert(index);
  }
});

下划线使用本机indexOf方法(如果可用),否则应用回退。因此,对于对象列表,您必须以其他方式实现它

一个例子可能是

_.chain(array).pluck("key").indexOf("value").value();


我强烈建议你看看lodash。它包含了相当多的漂亮的小函数,但不幸的是,它缺少这些函数

例如,这是您将使用lodash所做的:

var array = [{'id': 1, 'name': 'xxx'},
           {'id': 2, 'name': 'yyy'},
           {'id': 3, 'name': 'zzz'}];

var searchValue = {'id': 1, 'name': 'xxx'};


var index = _.findIndex(array, searchValue); 
console.log(index === 0); //-> true

此外,如果您必须使用下划线,您可以在以下位置获取lodash的下划线构建:


ES2015 随着ES2015的广泛使用(通过Babel等Transpiler),您可以放弃lodash和下划线来完成手头的任务,而使用本机方法:

var arr = [{ id: 1 }, { id: 2}];

arr.findIndex(i => i.id === 1); // 0

如果您有复杂的对象,并且希望在集合中搜索一个对象以查找某个属性,只需执行以下操作:

 _.indexOf(arrayObj, _.findWhere(arrayObj, {id: 1})  );
其中“arrayObj”是包含对象的集合,“id”是道具,“1”是搜索中的值。

我正在使用find index

 _.findIndex(array, searchValue); 

@T.J.Crowder是的,同意。修复了我的答案。您可能需要查看如何使用。:-)有趣。这里缺少下划线文档,因为它们没有解释谓词的参数是什么(它们只显示带有值的示例)。
_.find(array, function(item, index) {
  if (item.id == searchValue.id) {
    alert(index);
  }
});
 _.indexOf(arrayObj, _.findWhere(arrayObj, {id: 1})  );
 _.findIndex(array, searchValue);