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 不存在
4.includes()
const array = ['apple', 'banance', 'orange']
array.includes('banance') //true存在
array.includes('strawBally') //false存在
5.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')) //[] 不存在
网友评论