Iteration through sub-arrays

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:

Do you have some code started? That way would be easier to give you more specific hint. The general idea is to loop over the sub-array. Looking at version above, what is referring to the sub-arrays, and how it could be used in another loop?

Hi, well, to be honest I thought about a second loop because if I want to access to a specific item of sub-array I have to do end-up writing something like this:

arr[i][b]

because i access represents the group and b the item, but I’m not so sure, maybe there’s a less complicated way to perform this action on a function?

Sounds good, that’d get individual element from the sub-array. What’s next then?

I wouldn’t worry too much about things being complicated at this point. It might be easier to spot complicated things, once everything is working, and it’s easier to determine, whether specific potential optimization won’t break it again.

I’m getting somewhere:

function filteredArray(arr, elem) {
  let newArr = [];
 
  for (let i = 0; i < arr.length; i++) {
    let subArr = arr[i];
    for (let b = 0; b < subArr.length; b++) {
        if (subArr[b] != elem) 
        newArr.push(subArr[b]);     
     }
   }
   return newArr;
}

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

The output is:
[ 3, 3, 1, 6, 3, 3, 13, 26, 19, 3, 9 ]

So a single array, but still, the result is that I’m no longer eliminating a whole sub-array from the output and the 2 is gone (success!), mmm, I’d still like to have the structure array\sub-array intact tho.

Every element goes into newArr directly right now. How can that be changed to have them in new sub-arrays?

Yeah, I can’t figure that out, I’ve tried different things but I end up with sub-arrays that I don’t need.

What’s your updated code? It’s a bit similar as with the newArr, after looping over one sub-array, there should be one, new array.

I’ve tried with a new array outside of the second loop but obviously I end up with 4 times the same array (because of i that makes 4 loops), so I just went back to the old code, I’m stuck.

Without seeing code, I can only give more general suggestions. Slightly simplified version of it might look like:

for (let i = 0; i < arr.length; i++) {
  /* loop */
  newArr.push(/* array from loop */);
}

The number of sub-arrays that are pushed into the newArr should still be at most the same as in the original array.

I found a nice solution. Here you go:

function filteredArray(arr, elem) {
  let newArr = [];
  // Only change code below this line
  let subArr;
  for (let i = 0; i < arr.length; i++) {
    subArr = [];
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] !== elem) {
        subArr.push(arr[i][j]);
      }
    }
    newArr.push(subArr);
  }
  // Only change code above this line
  return newArr;
  }
  
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 2));

So it loops through the elements of the outer array (much like your original code did), but what makes this code special is that it loops through the elements of the subarrays as well.

so for the array [[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]] the for-loop in a for-loop would cause the iteration process to look something like this:

arr[0]
  arr[0][0]
  arr[0][1]
  arr[0][2]
arr[1]
  arr[1][0]
  arr[1][1]
  arr[1][2]
arr[2]
  arr[2][0]
  arr[2][1]
  arr[2][2]
arr[3]
  arr[3][0]
  arr[3][1]
  arr[3][2]
// representation of iteration process if there are two for-loops.

Instead of just:

arr[0]
arr[1]
arr[2]
arr[3]
// representation of iterative process if there is just one for-loop.

The if statement pretty much says "if the current element of the current subarray (a.k.a. arr[i][j]) is not equal to the unwanted number (a.k.a. elem) then add that element to subArr", which is just another way of saying "put anything that isn’t elem in subArr"

So with that logic, ever subarray is modified to become without an elem.

I probably didn’t do a very good job at explaining that, so if anything was confusing, let me know. Also feel free to ask questions and I’ll do my best to answer. Thank you for reading.

Ah, there you go! I thought about doing that too, in particular:

subArr.push(arr[i][j]);

newArr.push(subArr);

but I messed up and wasn’t able to set the whole thing properly (well, I’m still learning after all, ahah!). I’ll write this down on my notes so that I’ll remember it, thanks a lot!

I’m really happy to help.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.