-
判断数组中是否存在某个值
var arrData = ['html', 'css', 'javascript']; var value = 'css'; console.log(arrData.includes(value)); console.log(arrData.some(item => item === value)); console.log(arrData.indexOf(value) < 0 ? false : true); console.log(arrData.findIndex(item => item === value) < 0 ? false : true); console.log(arrData.find(item => item === value) !== undefined ? true : false );
var arrData = [ {name: 'html', checked: true }, {name: 'css', checked: true }, {name: 'javascript', checked: true } ] var value = 'css' console.log(arrData.some(item => item.name === value)); console.log(arrData.filter(item=> item.name === value)[0] ? true : false); console.log(arrData.find(item => item.name === value) ? true : false);
-