Home > Net >  Remove item from an array using a function
Remove item from an array using a function

Time:12-09

I'm trying to build a function that removes an item from an array. Both the array and item are configured using parameters pushing in when I call the function.

However it's not returning the expected [1,2,4] rather it's returning "not yet" a string I built into an if statment to return if it fails.

I can see in a console log the popped variable = 3 and the current for loop is correctly looping through all the options. So why isn't it working?

const removeFromArray = function() {
   let args = Array.from(arguments);
   let popped = args.pop();
   for (i = 0; i < args.length; i  ) {
      let current = args[i];
      if (current === popped) {
         console.log(args);
         return args;
      } else {
         console.log("not yet");
      }
   }
};


removeFromArray([1, 2, 3, 4], 3);

CodePudding user response:

I don't know why you don't use any build-in function of JS like

let removeFromArray = (arr, remove) => arr.filter(x => x != remove)

let filteredArray = removeFromArray([1, 2, 3, 4], 3)

But let's do it your way

const removeFromArray(arr, remove) {
  const items = [];
  
  for (const item of arr) {
    if (item != remove) items.push(item)
  }
  
  return items;
};


removeFromArray([1, 2, 3, 4], 3);

CodePudding user response:

  const removeFromArray = function (array, itemToRemove) {
    return array.filter(item => item !== itemToRemove);
  };

CodePudding user response:

Ok I've commented your code, the problems in it and made changes accordingly so that it work like you wanted it to:

const removeFromArray = function() 
{
   // arguments is not [1, 2, 3, 4, 3], but instead it's [[1, 2, 3, 4], 3] (length is 2, remember this later)
   let args = Array.from(arguments);

   // pop works correctly and returns 3
   let popped = args.pop();

   // here we cannot loop with args.length, as it is 2
   // if we change args.length to args[0].length, this will work
   for (i = 0; i < args[0].length; i  ) {

      // args[i] won't work here for the same reason args.length didn't work
      // because we're targeting a wrong thing
      // if we changes this to args[0][i], it will work
      let current = args[0][i];

      // After the changes, this if will work correctly
      if (current === popped) {
         // We can't just return args
         // A) we're once again targeting and wrong thing
         // B) we haven't removed anything yet

         // so lets change this to first splice the array (remove the wanted value)
         args[0].splice(i, 1);
         // and then return the array where the wanted value is removed
         return args[0];
      }
   }
};


const newArray = removeFromArray([1, 2, 3, 4], 3);

// output the returned new array where 3 is removed
console.log(newArray)

The main problem is that args does not contain what you thought it does (the numbers array), it is actually args[0] that does.

The other thing was that when you found the value you wanted to remove from the array, you never actually removed it. So here we use splice to actually remove the value before returning.

CodePudding user response:

This is how I would do it, since you said you wanted to modify the original array rather than create a new one, splice would be the correct tool to use.

function removeFromArray(arr, rem){
  while(~arr.indexOf(rem)){
    arr.splice(arr.indexOf(rem), 1);
  }
}

var arr = [1, 2, 3, 4, 3];
removeFromArray(arr, 3)
console.log(arr);

  • Related