美文网首页
free code camp摧毁函数,运用到argument以及

free code camp摧毁函数,运用到argument以及

作者: dirty_octopus | 来源:发表于2019-01-03 20:33 被阅读0次

Seek and Destroy
摧毁数组

金克斯的迫击炮!

实现一个摧毁(destroyer)函数,第一个参数是待摧毁的数组,其余的参数是待摧毁的值。
第一种解法:

function destroyer(arr){
    var args=Array.prototype.slice.call(arguments,1);
    var a=arr.filter(function(item,index,array){
        return args.indexOf(item)==-1;//运用array.filter()方法最后要返回布尔值
    })
    console.log(a);
  }  
  destroyer([1, 2, 3, 1, 2, 3], 2,3)

第二种解法:

function destroyer(arr){
    var args=Array.prototype.slice.call(arguments,1);//Array.prototype.slice.call()是把argument转化为一个数组,1是slice()方法里面的起始下标
    var a=arr.filter(function(val){
        for (var i=0;i<args.length;i++){
          if(val==args[i]){
            return false;
          }
        }
        return true;//注意等for循环遍历完了之后再返回
    })
    console.log(a);
  }  
  destroyer([1, 2, 3, 1, 2, 3], 2,3)

相关文章

网友评论

      本文标题:free code camp摧毁函数,运用到argument以及

      本文链接:https://www.haomeiwen.com/subject/miiqrqtx.html