如何使用参数对象从数组中删除元素-我的代码有什么问题

How to remove elements from an array using arguments object - what's wrong with my code?

本文关键字:我的 代码 问题 什么 元素 删除 参数 何使用 对象 数组      更新时间:2023-09-26

我正试图从作为参数传递给函数的数组中删除元素。我试图从数组中删除的元素是等于函数的以下参数的元素。像这样:destroyer([1, 2, 3, 1, 2, 3], 2, 3);,所以如果数组(驱逐舰的第一个参数)包含"2"answers"3",我希望它们被删除。所以调用函数应该返回[1,1]。

function destroyer(arr) {
  var args = [];
  for(var j = 1; j<arguments.length; j++){
      args.push(arguments[j]);
    for(var i = 0; i<arr.length; i++){
        if(args.indexOf(arr[i]) > -1){
            arr.splice(i, 1)
        }
    }
  }
  return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

我不明白为什么我的代码为destroyer([1, 2, 3, 1, 2, 3], 2, 3);工作(它返回[1,1]),但不为destroyer([2, 3, 2, 3], 2, 3);(它应该返回[],但返回[3])。

请告诉我为什么这不起作用。

我创建了一个repl。它在https://repl.it/BTu5

前面的答案是正确的,但我找到了一些有用的东西,它更类似于问题的代码,帮助他理解:代码:

 function destroyer(arr) {
 var args = [];
  //You want add all the nums you want remove from array, so you start from 1, which means second arg,first arg is the array you want to perform
  for(var j = 1; j<arguments.length; j++){ 
  //You store the args to an arg array
      args.push(arguments[j]);
    //Once you have the arg, you want loop the target array, see if the newly added arg exist in the target array, if it is, then remove it
    for(var i = 0; i<arr.length; i++){
        //Found it, remove it now! note: your found the index, then you need take it out, check the doc for slice function arr.slice([begin[, end]]) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
        if(args.indexOf(arr[i]) > -1){
            //key change, remove the element 
            arr.splice(i, i+1)
           }
       }
   }
  return arr;
  }
 destroyer([2, 3, 2, 3], 2, 3);

也为你创作了一个剧本

https://repl.it/BTu5/5

示例:slice(1,4)提取第二个元素到第四个元素(索引为1、2和3的元素)

在循环时更新集合是不好的编程实践:对arr.splice()的调用可能会改变arr.length的值。试着添加一条debugger;语句,看看程序运行时发生了什么。

这个版本从输入数组

中删除不需要的元素
/* Call like this:
 *   removeValues(list, 2, 3);
 *   removeValues(list, "foo", 4, "bar");
 */
function removeValues(collection) {
  var it = collection.splice(0); // clear + create iterator
  var unwanted = Array.prototype.slice.call(arguments, 1);
  var i;
  for (i = 0; i < it.length; i++) {
    if (unwanted.indexOf(it[i]) < 0) {
      collection.push(it[i]);
    }
  }
}

为什么不直接使用。filter()呢?

function destroyer(arr) {
  var badValues = Array.prototype.slice.call(arguments); //Construct an array of arguments
  badValues.shift(); //Get rid of the first ("good") array element
  return arr.filter(function(x) {return badValues.indexOf(x) == -1;}); //Make "bad" values false and thus — filter them.
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

我已经使用过滤器函数完成了这个挑战,尽管建议也使用'indexOf',以便通过要过滤的值比较数组中的值。试试这个)' ' ' '

function destroyer(arr) {
    // Remove all the values
    var temp = [];
    for (var i = 1; i < arguments.length; i++) {
        temp.push(arguments[i]);
        arr = arguments[0].filter(function(value) {
            return ( value !== temp[i - 1]) ;
        });
    }
    return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

我在freeCodeCamp上使用参数对象和Array.prototype.filter()完成了这个基本算法脚本挑战。希望能有所帮助

function destroyer(arr) {
var args = [];
//Creating array of arguments
for(var j=1;j<arguments.length;j++){
    args.push(arguments[j]);
}
//Filtering the arguments from array
arr = arr.filter(function(val){
    if(args.indexOf(val)==-1)
        return true;
return false;
});
return arr;

}

驱逐舰([1,2,3,1,2,3],2,3);

function destroyer(arr) {
  // Make a copy of arr by using .slice() and an empty arr2 to put the elements that you want to remove 
  let arr1 = arr.slice()
  let arr2 = [];
  // here just iterate starting from 1 because 0 is the array that you want to modifie and push() to arr2
  for (let i = 1; arguments.length > i; i++) {
    arr2.push(arguments[i]);
  }
  // And here just filter arr1 with arr2, and that basically removes all arr1 elements that are present in arr2
  return arr1.filter(elem => !arr2.includes(elem));
}    
destroyer([1, 2, 3, 1, 2, 3], 2, 3);