const removeFromArray = function(firstArray,...toRemove) {
let modifiedArray = [...firstArray];
for (let i = 0; i < toRemove.length; i++) {
if (modifiedArray.includes(toRemove[i])) {
modifiedArray.splice(modifiedArray.indexOf(toRemove[i]), 1)
}
}
return modifiedArray;
};
console.log(removeFromArray([3,4,5], 3,5)) ; //result is [4]
Goal : to take any array as first argument and remove whatever you want from said array as next argument.
Sorry this is keeping me from sleeping but what is [i]
doing attached toRemove
?
Lets say I want to do removeFromArray(['a','b','c'], 'b');
This will mean toRemove
is equal to 'b'. The length of toRemove will be 1. So the loop says run for one iteration because toRemove is only 1 length.
To this point I think I understand. I don't get why (modifiedArray.includes(toRemove[i]))
because doesn't that just mean (modifiedArray.includes('b'[1]))
?
And if you do a like removeFromArray(['a','b','c'], 'a', 'c');
Would the [i]
mean that toRemove
will get iterated twice, once for each, so for the second iteration its value would be 2?
(modifiedArray.includes(['a', 'c'][1]))
(modifiedArray.includes(['a' ,'c'][2]))
or would it be
(modifiedArray.includes(['a' ,'c'][1]))
(modifiedArray.includes(['a' ,'c'][1]))
I hope I was able to kind of explain my confusion I'm a bit frustrated. Thanks!