0

I'm trying to remove multiple elements from an array with .splice() but I am not getting the desired output, any suggestions ?

const removeFromArray = function(arr, ...target) { //taking target as a rest parameter.
    for(let i = 0; i < arr.length; i++ ){
        for(let j = 0; j < target.length; j++){
            if(arr[i] == target[j] && typeof(arr[i]) == typeof(target[j])){
                arr.splice(i , 1); //removing 1 element from i'th index.
            }
        }
    }
    return arr;
}; 

console.log((removeFromArray([1, 2, 3, 4], 3, 2))) //[ 1, 3, 4 ] , should display [1,4]
  • 1
    logically, do you mean to this: [1, 2, 3, 4].filter(num=>![3,2].includes(num)) – Oleg May 03 '22 at 05:34
  • Do you really need it to be using splice? – NightEye May 03 '22 at 05:39
  • @Octavia No, it was the first method that came to my mind, however now it's resolved, I used ` array.push() ` to push to a new array except the elements which I wanted to remove. – Arjun Pathak May 04 '22 at 07:50

0 Answers0