let arr = [
{name:'test1',age:1},
{name:'test2',age:2},
{name:'test3',age:3}
]
let temp = arr.findIndex(function(item){
console.log(item.name)
return item.name == 'test3'// 这里return不可少,必须“==”
})
console.log('temp=',temp)
// var arr = [12, 16, 18, 20]
// let index = arr.findIndex(item => item == 16)
// console.log('index=',index)//1
let arr = [
{name:'test1',age:1},
{name:'test2',age:2},
{name:'test3',age:3}
]
// 需求:获取name=test3的元素下标
var index = arr.findIndex(item => {return item.name == 'test3'})
console.log(index)
// 需求:获取name=test3的元素
var obj = arr.find(item => {return item.name == 'test3'})
console.log(obj)
标签:es6,findIndex,name,index,age,arr,item,test3,find
From: https://www.cnblogs.com/pansidong/p/16622558.html