I need to delete a subarray if it has a particular element.
function filteredArray(arr, elem) {
let newArr = [];
for (let i = 0; i < arr.length; i++){
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] === elem) {
arr.splice(i--, 1);
newArr = [...arr]
}
}
}
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 19));
The code above works fine if the element is found just once (for ex. 19). But if I try number 3, I get: Uncaught TypeError: Cannot read property 'length' of undefined. Can someone please explain what is happening here?