-2

I am using the forEach() on the array that is passed through. The function I am passing into forEach() is if the element of arrays does not match with one of the arguments given, then push it into the newArray.

However, I keep getting a blank array, when I want to see newArray = [1, 2].

I am thinking there is something wrong with my if statement, but I really don't see what the problem is. I can't seem to put a finger on the issue.

const removeFromArray = function(arrays, ...args) {
    const newArray = [];
    
    for (const arg of args) {
        if (arrays.forEach((element) => element !== arg))
        newArray.push(element);        
    }
    return newArray;
}

removeFromArray([1,2,3], 3);
M Codet
  • 57
  • 5
  • 4
    [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) exists. – CBroe Mar 09 '23 at 14:31
  • 2
    `array.forEach()` doesn't return anything. – mykaf Mar 09 '23 at 14:32
  • 1
    don't do this kind of code, use `Array.filter()` method – Mister Jojo Mar 09 '23 at 14:32
  • 1
    Not sure exactly what your goal is here but if `Array.filter()` isn't what you're looking for also check out `Array.includes()` – Portal Mar 09 '23 at 14:34
  • 1
    Thanks all for the suggestions. I am misunderstanding forEach(). Will reread the docs. – M Codet Mar 09 '23 at 14:42
  • Does this answer your question? [How can I remove a specific item from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array-in-javascript) – Wyck Mar 09 '23 at 14:43
  • @wyck That is a good resource! For my question, I wanted to know why I am wrong so I can dash or revise my understanding. Thanks for your help. – M Codet Mar 09 '23 at 14:50
  • 1
    weird this was closed. – terpinmd Mar 09 '23 at 15:01
  • 1
    Also the `args` parameter was presented as `...args` so presumably that means it's intended to work such that you can remove all the items that were provided as arguments. e.g. `removeFromArray([1, 2, 3, 4], 1, 3)` I expect should return `[2, 4]`. If so, the technique should be `array.filter(item => !args.includes(item))` – Wyck Mar 09 '23 at 15:08

2 Answers2

0

Try using Array.filter() method

const removeFromArray = function(arrays, args) {
    return arrays.filter(ele => ele !== args)
}

removeFromArray([1,2,3], 3);
Srushti Shah
  • 810
  • 3
  • 17
0

I will assume that you are doing this for a learning experience. As others have said, filter is what you should be using. With that said you had a couple errors in your code.

  1. You did if(array.forEach... which can not be evaluated as its a iterator and is not truthy
  2. You needed to push the element inside the forEach, not outside if it

const removeFromArray = function(arrays, ...args) {
    const newArray = [];
    
    for (const arg of args) {
        arrays.forEach(element => {
           if(element !== arg){
               newArray.push(element);               
           }
        })
    }
    return newArray;
}

console.log(removeFromArray([1,2,3], 3));
terpinmd
  • 1,014
  • 8
  • 15
  • 1
    oops, this is backwards logic. The expected result is `[1, 2]` you meant `element !== arg` – Wyck Mar 09 '23 at 14:41
  • Thanks! I am self-studying the odin project. it's been rough. In my original code, I did if (arrays.forEach...), but I can't seem to grasp why that is wrong. To me it reads like this: if an element in the `arrays` does not equal an `arg` THEN push it in. But this is not how it's being interpreted? – M Codet Mar 09 '23 at 14:46
  • 1
    @MCodet arrays.forEach is a iterator, like a program flow. doing if(array.forEach wouldnt be that unlike doing if(switch.... or even if( if ( . Now, you might do something like if(array.length...... . Put another way whatever is in the if(...) needs to be something that can be evaluated. forEach can not be evaluated for truthyness – terpinmd Mar 09 '23 at 14:51
  • @terpinmd My wow. That makes sense. Thank you so much. I suppose I will have to be mindful of whether methods can be evaluated for a Boolean or perform some other purpose that I hope to learn. Thanks again! – M Codet Mar 09 '23 at 14:54
  • @MCodet happy coding! If the answer and comments are suitable pls consider accepting this as the answer :) – terpinmd Mar 09 '23 at 14:57
  • @terpinmd sorry, I am a bit late to the reply, but I accepted your answer. Got to get used to accepting answers :) Not sure why someone closed the question, but I'll try to formulate better questions next time if mine was too simple. Thanks, again for your help! – M Codet Mar 13 '23 at 08:48