我想是这样的:如何比较具有相同值的两个数组并将相同的值存储在新数组中?
var diff = $(array1).not(array2).get();
console.log(diff); // store the difference
而更换.not()
与.is()
但没有work..which产生错误。它只存储差异,但我只想要存储在新数组中的相同值。我如何在jQuery中做到这一点,而不管它在两个数组中的长度大小?
var array1 = ['a','b','c','d','e','f','g'];
var array2 = ['c', 'b'];
var sameValArr = [];
// TODO:
// 1. compare the two arrays if there's any matching values in both
// 2. if yes, store the matching value in a new array; if no, do nothing
// 3. check the new array if it isn't empty
// 4. if empty, hide the video; if not empty do nothing (show video)
for(var i = 0; i < array1.length; i++)
{
for(var j = 0; j < array2.length; j++)
{
if(array1.indexOf([i]) === array2.indexOf([j]))
{
sameValArr.push(array1[i]);
console.log("sameValArr: ", sameValArr);
}
}
}
使用indexOf()
方法提供给此问题的答案无效。
您是否尝试过.contains()或.indexOf()? – mhodges
真的不清楚数组和'$'''''' – charlietfl
@charlietfl有什么关系他们实际上并没有,我删除并更新了帖子,对此抱歉。 – TheAmazingKnight