我试过了
function removeFromArray(manyMoreArgs, number) {
let i = 0;
while (i < manyMoreArgs.length) {
if (manyMoreArgs[i] === number) {
manyMoreArgs.splice(i, 1);
} else {
i++;
}
}
return manyMoreArgs;
}
console.log(removeFromArray([1, 2, 3, 4], 3)); // result = [1, 2, 4] this removes 3 from array. it works! but then >>
console.log(removeFromArray([1, 2, 3, 4], 3, 2)); // result = [1, 2, 4] this removes 3 from array too but I also want to remove 2 from array
如果我想从数组中删除数字,我应该怎么做?
3条答案
按热度按时间ecfsfe2w1#
您可以将
numbers
参数定义为数组或者作为variadic argument
unftdfkk2#
我想你可以使用一个JavaScript函数,接收这两个数组,过滤掉第一个数组,只包含第二个数组中没有的元素,然后返回过滤后的数组。
0dxa2lsx3#
以下是我的版本