Hi people, I completed this challenge and the code works fine, I understand everything that’s happening so that’s good.
But I was wondering, what if I wanted to iterate through every single element of the sub-arrays and extract ALL the items except the parameter elem?
Let me show you, at the moment the function is like this:
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) == -1) {
newArr.push(arr[i]);
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 2));
The Output is:
[ [ 1, 6, 3 ], [ 3, 13, 26 ], [ 19, 3, 9 ] ]
But I want:
[[3, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]]
In other words I want to keep everything except elem (in this case 2). I thought it could be accomplished with a second for loop, but so far I wasn’t able to write anything that works.
Can someone explain to me how I could do this?
Thanks!
Challenge: Iterate Through All an Array’s Items Using For Loops
Link to the challenge: