【freeCodeCamp Intermediate】Seek and Destroy 查找和消灭

本文介绍了一个名为destroyer的函数实现,该函数接受一个数组作为第一个参数,随后接收一个或多个参数。其功能是从初始数组中移除所有与这些额外参数值相同的元素,并返回处理后的数组。

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Note: You have to use the arguments object.

我们要写一个叫destroyer的函数。传给它的第一个参数是数组,我们称他为初始数组。后续的参数数量是不确定的,可能有一个或多个。你需要做的是,从初始数组中移除所有与后续参数相等的元素,并返回移除元素后的数组。

你必须使用arguments对象

function destroyer(arr) {
  let arg = Array.from(arguments).slice(1)
  let newArr = arr.slice();
  for(let i = 0;i< arg.length;i++){
    newArr = newArr.filter(val=>{
      return val !== arg[i]
    })
  }
  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
function destroyer(arr) {
  let arg = Array.from(arguments).slice(1)
  return arr.filter(item => !arg.includes(item))
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值