Remove elements from an array with "filter" in javascript

2

I've done the following freeCodeCamp exercise. It involves eliminating elements of a array according to numbers received in parameter.

In the call to the function destroyer a array appears and, as second and third parameters, the numbers that must be deleted from said array.

This is the code:

function destroyer(arr) {
  var args = Array.prototype.slice.call(arguments);
  args.splice(0, 1);
  return arr.filter(function(element) {
    return args.indexOf(element) === -1;
  });
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

The first instruction is clear: convert the arguments of the function into a real array in order to work with it.

The second instruction separates the first argument to keep the numbers to search. But when I debug the process, I check that arr, the parameter received by the destroy function, only contains [1,2,3,1,2,3]

Why does this happen? Should not it contain the complete parameter that was sent when invoking the function? that is: ([1, 2, 3, 1, 2, 3], 2, 3) .

That's why I do not understand the third part, when the filter is applied on the arr parameter. At what point did the arguments 2,3 of destroyer disappear from the parameter received by arr ?

And one last question. Why are there two returns? Is one the response of the callback and the other is the answer of destroyer?

    
asked by cga 08.02.2017 в 13:56
source

3 answers

3

I hope this serves as your orientation

function destroyer() {
  // Obtengo los Parametros de la funcion, en este caso 3
  var args = Array.prototype.slice.call(arguments);
  // Dejo en la variable arr el arreglo a intervenir
  var arr = args[0];
  
  // Retorna resultado de la funcion en un arreglo de acuerdo de lo que devuelva el filtro e indexOf
  return arr.filter(function(element) {
    // index of devuelve el indice de la busqueda cuando encuentra un valor, o -1 cuando no encuentra una coincidencia. Osea que el valor 1 al no encontrarlo este retornara -1 
    return args.indexOf(element) === -1;
  });
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
    
answered by 08.02.2017 в 14:35
2

What happens is that the function filter returns an array different from the original. That's why when you do arr.filter you're literally filtering the array and staying with the new array already filtered since that's what you're returning.

return arr.filter

Think that instead of returning the new array you could assign it to a variable:

var newArray = arr.filter(function(element) {
    return args.indexOf(element) === -1;
});

but in your case you are assigning it to return .

Now the number of returns is because the filter function needs a return and the other one is the function destroyer .

    
answered by 08.02.2017 в 14:30
1
  

"When I debug the process, I check that arr, the parameter received by the destroyer function, only contains [1,2,3,1,2,3] Why?"

arr contains only that array because the function destoyer receives a single parameter and you pass it to 3. So arr is only the first.

  

"At what point did you disappear from the parameter received by destroying the 2.3 arguments of arr?"

the variable args is the one that contains the 2nd and 3rd parameters (after doing splice of the first parameter) since you get them from the call, not the ones that the function receives.

You apply the filter on arr (that contains the array with the data) but you make a filter looking for args

the function filter returns from the array arr all values not found in the array args . That's why in the log (I have edited your question to print the result of destroyer ) you see that if you leave the values 1 and 1 .

  

"Why are there two returns?"

The first return is that of the function destroyer and the other is that of the function within filter

    
answered by 08.02.2017 в 14:27