How to delete an item from a nested array using splice method when a particular element is present in the nested array

We have defined a function, filteredArray , which takes arr , a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr . Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed

Your code so far


function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
 for(let i = 0; i<arr.length; i++){
   let result = arr[i].indexOf(elem);
     if(arr[i].indexOf(elem)!=-1){
     let answer = arr.splice(arr[i][result],1)
     console.log(answer)
     }
     
     else{
     return newArr = [...arr];
     
     }
     //console.log(arr[i].indexOf(elem))
 }
// Only change code above this line
return newArr;

}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

you have copied the challenge description. Instead, why don’t you ask your question and describe what’s the issue?

1 Like