目前想到的方法有这么几个
1.indexOf() -> ES5
const array = ['apple', 'banance', 'orange']
array.indexOf('apple') // 0 存在
array.indexOf('strawBerry')
// -1不存在
2.find() -> ES6
const array = ['apple', 'banance', 'orange']
array.find(obj => obj == 'banance')
//banance 存在
array.find(obj => obj == 'strawBally')
//undefined 不存在
const arrayList = [
{name: '张三'}, {name: '李四'}
]
arrayList.find(obj => obj.name == '李四') //{name: '李四'} 存在
arrayList.find(obj => obj.name == '王五') //undefined 不存在
3.findIndex() -> ES6
const array = ['apple', 'banance', 'orange']
array.findIndex(obj => obj == 'banance')
//大于0存在
array.findIndex(obj => obj == 'strawBally')
//-1 不存在
3.includes()
const array = ['apple', 'banance', 'orange']
array.includes('banance')
//true存在
array.includes('strawBally')
//false存在
4.filter()
const array = ['apple', 'banance', 'orange']
array.filter(obj => obj == 'orange')
//['orange'] 存在
array.filter(obj => obj == 'strawBally')
//[] 不存在
const array = [{ name: 'banance' }, { name: 'apple' }]
console.log(array.filter(obj => obj.name === 'apple'))
//[{name: 'apple'}] 存在
console.log(array.filter(obj => obj.name === 'strawBally'))
//[] 不存在
来源:oschina
链接:https://my.oschina.net/u/4265059/blog/3554698