标签:obj undefined bsp nan awb range OLE const indexof
目前想到的方法有这么几个
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‘)) //[] 不存在
js在数组中查找是否存在某一个数值
标签:obj undefined bsp nan awb range OLE const indexof
原文地址:https://www.cnblogs.com/gqx-html/p/10791476.html