Home > Blockchain >  Remove Multiple values from an Array - Odin Project
Remove Multiple values from an Array - Odin Project

Time:10-13

I am trying to do the 4th Javascript excercise in The Odin Project (https://github.com/TheOdinProject/javascript-exercises/tree/main/04_removeFromArray) and i am facing huge difficulties.

Currently the only thing that i managed to do is pass the first test, which was about to remove one value from the array. My code is the one below:

let array = new Array();

const removeFromArray = function(array) {
   let index = array.indexOf(3);
   if (index > -1) {
       array.splice(index,1);
      }
return array;
}

Any help on how i could advance on the level that i could remove multiple values, that would be amazing.

I ve read some solutions online but i really didn't get a lot.

Thank you very much in advance!

CodePudding user response:

Let's say you want to remove multiple values that are passed as an array argument to your function. Then one possible solution would be something like this:

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

console.log(removeFromArray([1, 2, 3, 4], [2,3]))

In your console you can see that the resulting array is [1,4]. There are may ways to solve this kind of problem. Also, As, already suggested in the comments above, you should have taken an argument for the first part of the assignment too. The solution would be something like this:

function removeFromArray(array, arg) {
    
    const result = array.filter(element => element !== arg);
    return result
    
}

console.log(removeFromArray([1, 2, 3, 4], 3))

The result is of course [1,2,4].

CodePudding user response:

Let's approach this problem by thinking of what we want our function to do and what we need to pass our function for that.

Here is our desired function as pseudo-code:

myArray= ["one", "two", "three", "four"]

function removeFromArray(array, index) {
  return array without element in given index
}

And calling removeFromArray(myArray, 3) should return ["one", "two", "four"].

Awesome. Now that we know what we are aiming to achieve, implementing it should be a breeze.

You seem to have approached this problem with the splice function. Here is a way to do it that is semantically more clear:

  let arr = ["zero", "one", "two", "three", "four"];

  function removeIndex(i, array) {
    let newArray = [];
    for (let j = 0; j < array.length; j  ) {
      if (j !== i) {
        newArray.push(array[j]);
      }
    }
    return newArray;
  }
  console.log(removeIndex(2, arr));

CodePudding user response:

Solution:

const removeFromArray = (arr, ...args) => arr.filter((item) => !args.includes(item))

Explanation:

(arr, ...args) - I'm accepting input array as first argument and spreading any other arguments using spread operator (...)

=> - I'm using arrow notation to return value without the need of using return keyword

arr.filter() - I'm using build in filter method to return filtered array based on given restrictions

!args.includes(item) - I'm checking each element of the input array against the spreaded array of arguments

  • Related