说我有2个阵列:
var array1 = [1, 2, 3];
var array2 = [1, 2, 3, 4];
如何将它们与单个元素进行比较(在本例中为数字4)?
类似的东西:
if ( array1 == array2 ) {
//then do this
}else{
// find out the one that isn't the same in each and show it here
}
答案 0 :(得分:2)
您可以使用Array.prototype.find执行相同的操作
检查此代码段
var array1 = [1, 2, 3];
var array2 = [1, 2, 3, 4];
function findNumber(number) {
return number=== 4?true:false;
}
var number=array2.find(findNumber);
if(number)
console.log("found");
else
console.log("not found");
希望有所帮助