實(shí)現(xiàn)一個(gè) destroyer 函數(shù),第一個(gè)參數(shù)是初始數(shù)組,后跟一個(gè)或多個(gè)參數(shù)。從初始數(shù)組中刪除與這些參數(shù)具有相同值的所有元素。
function destroyer(arr) {
//取出所有參數(shù)
var args = Array.from(arguments);
//剔除arr后的參數(shù)
var tempArr =args.slice(1,args.length);
return arr.filter(function(item){
var result=true;
for(var i=0;i<tempArr.length;i++){
if(tempArr[i]===item)
{
result=result&&false;
}else
{
result=result&&true;
}
}
return result;
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 1, 2, 3], 2, 3) 應(yīng)該返回 [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) 應(yīng)該返回 [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) 應(yīng)該返回 [1].
destroyer([2, 3, 2, 3], 2, 3) 應(yīng)該返回 [].
destroyer(["tree", "hamburger", 53], "tree", 53) 應(yīng)該返回 ["hamburger"].