如何通过检查 JavaScript 中的属性值来查找数组中对象的索引?

Sou*_*war 4 javascript arrays angularjs

我有一个像这样的数组:

$scope.myArray = [{
  columnName: "processed1",
  dataType: "char"
}, {
  columnName: "processed2",
  dataType: "char"
}, {
  columnName: "processed3",
  dataType: "char"
}];
Run Code Online (Sandbox Code Playgroud)

我想找到index哪个object属性值满足“processed2”

我该怎么做?我尝试使用array.indexOf()方法但得到响应 -1

Ray*_*yon 5

使用Array#findIndex如果数组中的元素满足提供的测试函数,则该方法返回数组中的索引。findIndex()否则返回-1。

Array#indexOf将失败,因为arraycontains objects,使用and is equalsindexOf()测试元素是否引用相同triple-equals operatorobjectobjectmemory-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);
Run Code Online (Sandbox Code Playgroud)