我有一个像这样的数组:
$scope.myArray = [{
columnName: "processed1",
dataType: "char"
}, {
columnName: "processed2",
dataType: "char"
}, {
columnName: "processed3",
dataType: "char"
}];
我想找到 object
的 index
属性值满足 "processed2"
我该怎么做?我尝试使用 array.indexOf()
方法,但我得到了 response -1
最佳答案
Use
Array#findIndex
, ThefindIndex()
method returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.
Array#indexOf
将失败,因为 array
包含 objects
,indexOf()
使用 测试元素>triple-equals operator
和 object
等于 object
如果它引用相同的 memory-location
var myArray = [{
columnName: "processed1",
dataType: "char"
}, {
columnName: "processed2",
dataType: "char"
}, {
columnName: "processed3",
dataType: "char"
}];
var index = myArray.findIndex(function(el) {
return el.columnName == 'processed2';
});
console.log(index);
关于javascript - 如何通过检查 JavaScript 中的属性值来查找数组中对象的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972974/