0

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);
9
  • 1
    You'll do yourself a favour if you indent your code properly. Commented Dec 8, 2021 at 20:56
  • 1
    why not just use indexOf/splice? Commented Dec 8, 2021 at 20:58
  • 2
    Why do you not just declare your parameters in the function header? Note that the array that you passed as first argument is found in arguments[0]. You don't look there. You seem to think that arguments has many entries, including those of the array that you pass, but it only has two entries: the (nested) array, and the value. Commented Dec 8, 2021 at 20:59
  • In your example, args.length will be 1 because there is only one argument left (the array) once you pop off the 3. I think you meant to iterate over args[0], not args. Commented Dec 8, 2021 at 21:00
  • because your for loop is looping through the first argument in arguments, which is an array for (i = 0; i < [[1,2,3,4]].length; i++) so the comparison is if ([1,2,3,4] ===3) Commented Dec 8, 2021 at 21:00

4 Answers 4

2

Ok I've commented your code, the problems in it and made changes accordingly so that it works 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 change 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, really appreciate all the comments. That makes sense, like you said the main issue was the arguments length was 2 not 5 hence the rest of the code did not work.
No problem! Do make a note that there are indeed better ways to implement something like this, like others have shown, but it never hurts to try things in your own way. I wanted to show how you'd make this specific implementation work the correct way :)
1
  const removeFromArray = function (array, itemToRemove) {
    return array.filter(item => item !== itemToRemove);
  };

2 Comments

Thanks, would you mind explaining this to me? Arrow functions are new to me. Seems to be returning another function which says if item (where is this declared?) is not the same as the second parameter then filter it?
Sure! Arrow function is easy. const func = (item) => item != 3 is the same as function func (item) { return item != true ;} The method 'filter' of arrays invokes a function for each of items. If the function returns true, then this element remains in the array. We could also write something like this array.filter(function (item) { return item != 3; });
1

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);

1 Comment

Thanks. I was coidng PHP, and took a break...
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.