Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Remove item from an array using a function

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);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

  const removeFromArray = function (array, itemToRemove) {
    return array.filter(item => item !== itemToRemove);
  };
about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!