0

for example this is my array of arrays:

const arr = [[1, 2, 3, 4], [6, 6, 7, 8, 9], [10, 11, 12, 13, 14]];

How can I remove the first index, so I get the array of arrays, but excluding the first index?

So filteredArr = [[6, 6, 7, 8, 9], [10, 11, 12, 13, 14]]; would be final output.

Thanks.

NOTE: I don't want to mutate array.

storagemode11
  • 875
  • 7
  • 11

1 Answers1

4

If you want to get a new array, and not mutate the original array.

Options 1: Use destructuring with rest to get a new array with all items, but the 1st:

const arr = [[1, 2, 3, 4], [6, 6, 7, 8, 9], [10, 11, 12, 13, 14]];
const [, ...filteredArr] = arr;

console.log(filteredArr);

Option 2 : use Array.slice() to get all items after the 1st (index 0):

const arr = [[1, 2, 3, 4], [6, 6, 7, 8, 9], [10, 11, 12, 13, 14]];
const filteredArr = arr.slice(1);

console.log(filteredArr);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209