We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这是ES7新增的判断数组里面是否存在某个值的方法,存在时返回true,否则返回false。 例如:const arr=[1,3,4,5,6] ,那么arr.includes(3)将返回true,因为包含了数字3 值得注意的是,如果该值是一个非空对象,那么无论怎么判断,都是返回false的。
const arr=[{name:'zls'}] console.log(arr.includes({name:'zls'})) // false,注意这里是返回false
其它基本数据类型都是返回true,所有测试用例如下:
const arr=[1] console.log(arr.includes(1)) // true const arr1=[true,false] console.log(arr1.includes(true)) //true const arr2=[undefined] console.log(arr2.includes(undefined)) // true const arr3=['str'] console.log(arr3.includes('str')) // true const arr4=[null] console.log(arr4.includes(null)) // true const arr5=[{name:'zls'}] console.log(arr5.includes({name:'zls'})) // false
如果我们要判断数组里是否存在某个对象,我们可以使用arr.find()或者arr.findIndex(), 例如 const arr=[{name:'zls'}],如果要判断这个arr数组里是否包含 {name:'zls'},可以这样:
const index=arr.findIndex((item)=>{ return item.name='zls' }) // index为0,在第0个位置,所以是存在的
The text was updated successfully, but these errors were encountered:
No branches or pull requests
这是ES7新增的判断数组里面是否存在某个值的方法,存在时返回true,否则返回false。
例如:const arr=[1,3,4,5,6] ,那么arr.includes(3)将返回true,因为包含了数字3
值得注意的是,如果该值是一个非空对象,那么无论怎么判断,都是返回false的。
其它基本数据类型都是返回true,所有测试用例如下:
如果我们要判断数组里是否存在某个对象,我们可以使用arr.find()或者arr.findIndex(),
例如 const arr=[{name:'zls'}],如果要判断这个arr数组里是否包含 {name:'zls'},可以这样:
The text was updated successfully, but these errors were encountered: