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

129
Views
How to implement a function that takes an array and some other arguments then removes the other arguments from that array

Please guys, this is an exercise (exercise 04) from Odin Project. I don't know if there is anything wrong with the code below, it keeps failing the 'npm test' (a test in the terminal) but then i checked it with console.log and it worked.

const removeFromArray= function(...args) {
    const array = args[0];
        const newArray = [];
             array.forEach((item) => {
                 if(!args.includes(item)) {
                      newArray.push(item);  
        }
        });
        return newArray;
    };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It seems to me that I have not understood the question very well. But JS has built-in functions that serve the purpose

Array map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Array filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

But from what I see in the code you have to remove the first element from the array.

I suggest you take a look at this question

remove first element from array and return the array minus the first element

about 4 years ago · Juan Pablo Isaza Report

0

I'd suggest using Array.filter() in combination with Array.includes() to achieve the required result.

We filter any element from the input array if it is present in the itemsToRemove array.

function removeFromArray(array, itemsToRemove) {
    return array.filter(element => !itemsToRemove.includes(element));
}

let array = [1,2,3,4,5,6];
let itemsToRemove = [2,4,6];

console.log(removeFromArray(array, itemsToRemove))

We can do this in a more efficient manner if itemsToRemove is very large, using a Set:

function removeFromArray(array, itemsToRemove) {
    const itemsToRemoveSet = new Set(itemsToRemove);
    return array.filter(element => !itemsToRemoveSet.has(element));
}

let array = [1,2,3,4,5,6];
let itemsToRemove = [2,4,6];

console.log(removeFromArray(array, itemsToRemove))

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!