根据对象的某个属性获得其在数组中的索引值

157 阅读1分钟

项目中经常会对数组数据进行操作,例如下面例子,JavaScript根据对象的某个属性获得其在数组中的索引值

let arr = [{id:123,name:"apple"},{id:456,name:"watermelon"}]
let index = arr.findIndex( item => item.name == "watermelon")
console.log(index)     //  1