我想我搞砸了某个地方,这是我的代码。
var flag;
for (i = 0; i < A.length; i++)
{
flag = 0;
for (j = 0; j < B.length; j++)
{
if (A[i].indexOf(B[j]) != -1)
{
flag = 1;
}
}
if (flag == 0)
{
A.splice(i, 1);
}
}
它给出的输出不符合我的需要 有人请帮帮我
最佳答案
我会这样做;
//returns intersection of multiple arrays
Array.prototype.intersect = function(...a) {
return [this,...a].reduce((p,c) => p.filter(e => c.includes(e)));
};
var a = [0,1,2,3,4,5],
b = [4,5,6,7,8,9];
a = a.intersect(b);
console.log(a);
关于javascript - 有一个字符串数组 A 和一个字符串数组 B 。我想删除A中不在B中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655350/