DavidSzczesniak
10/29/2017 - 12:44 AM

Remove specific parts of an array

Removes all elements from the initial array that are of the same value as the consequent arguments.

function destroyer(arr) {
  // Creating temporary array that will display the whole value, because only the first argument is printed by default
  var args = Array.prototype.slice.call(arguments);
  // Splices out first argument
  args.splice(0, 1);
  var placeHolder = [];
  
  // Iterate through both arr and args and see if contents match
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < args.length; j++) {
      // If yes, delete those from arr 
      if (arr[i] == args[j]) {
        delete arr[i];
      }
    }
  }
  
  // Filter out falsy variables that came up after deleting 
  placeHolder = arr.filter(removeFalseVar);
  return placeHolder;
  
}

// Method to filter falsy variables
function removeFalseVar(value) {
  return Boolean(value);
}

// Example call (arr = our array/first argument, args = consequent arguments)
destroyer([1, 2, 3, 1, 2, 3], 2, 3);