How to delete repeated nested arrays

Say you got this array:

finalInventory = [ [ [ 21, 'Bowling Ball' ],
    [ 2, 'Dirty Sock' ],
    [ 1, 'Hair Pin' ],
    [ 5, 'Microphone' ] ],
  [ 2, 'Hair Pin' ],
  [ 2, 'Hair Pin' ],
  [ 2, 'Hair Pin' ],
  [ 3, 'Half-Eaten Apple' ],
  [ 3, 'Half-Eaten Apple' ],
  [ 3, 'Half-Eaten Apple' ],
  [ 3, 'Half-Eaten Apple' ],
  [ 67, 'Bowling Ball' ],
  [ 67, 'Bowling Ball' ],
  [ 67, 'Bowling Ball' ],
  [ 7, 'Toothpaste' ],
  [ 7, 'Toothpaste' ],
  [ 7, 'Toothpaste' ],
  [ 7, 'Toothpaste' ] ]

You want to just return finalInventory but with NO repeated nested arrays. Basically this:

finalInventory = [ [ [ 21, 'Bowling Ball' ],
    [ 2, 'Dirty Sock' ],
    [ 1, 'Hair Pin' ],
    [ 5, 'Microphone' ] ],
  [ 2, 'Hair Pin' ],
  [ 3, 'Half-Eaten Apple' ],
  [ 67, 'Bowling Ball' ],
  [ 7, 'Toothpaste' ], ]

This is my approach:

return finalInventory.map((arr) => {
    finalarrays = []
    for (let v=0; v< arr.length;v++){
      if (arr[v][1] === arr[v+1][1]){
        // I am not sure what to put here
      }
      else {
        finalarrays.push(arr[v])
      }
    }
    return finalarrays
  })
  

However I am not sure what to do…I know the approach v+1 is probably pretty newbie but I haven´t come up with anything else…please help me. I have researched stackoverflow but i haven´t found any solutions to nested arrays

I was just googling this exact same problem! I decided to use the splice method for my purposes. See the section called “Removing Array Items By Value Using Splice.”

1 Like

I know you don’t want to hear this, but why not try to create a solution that does not create the duplicate sub arrays.

What you have here is a bit more complicated, because you don’t just have an array of duplicate sub arrays. Instead, you have some kind of 3-dimensional array which has some sub arrays which contain more sub arrays.

1 Like