admin 管理员组文章数量: 1086019
I have an array like:
$scope.myArray = [{
columnName: "processed1",
dataType: "char"
}, {
columnName: "processed2",
dataType: "char"
}, {
columnName: "processed3",
dataType: "char"
}];
I want find the index
of object
which property value satisfy "processed2"
How can I do it? I tried using array.indexOf()
method but I got response -1
I have an array like:
$scope.myArray = [{
columnName: "processed1",
dataType: "char"
}, {
columnName: "processed2",
dataType: "char"
}, {
columnName: "processed3",
dataType: "char"
}];
I want find the index
of object
which property value satisfy "processed2"
How can I do it? I tried using array.indexOf()
method but I got response -1
2 Answers
Reset to default 5Use
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
will fail as array
contains objects
, indexOf()
tests the element using triple-equals operator
and object
is equals to object
if it refers to same 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);
You can use a simple for loop.
for(var i=0;i < $scope.myArray.length; i++)
{
if($scope.myArray[i].columnName == 'processed2') {
// Do something with found item
break;
}
}
本文标签:
版权声明:本文标题:angularjs - How to find the index of an object in an array by checking property value in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744087223a2531354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论