How to get arrays inside an array Javascript

Loading

Get Array Inside an Array in Javascript, How to fetch an array inside Javascript complete explained

javascript

Loading

In JavaScript, arrays can contain other arrays, creating a multi-dimensional array or an array of arrays. To access elements inside an array of arrays, you use multiple bracket notation. Here’s a guide on how to get and manipulate arrays inside an array:

1. Creating an Array of Arrays

You can create an array of arrays by nesting arrays within a main array:

let arrayOfArrays = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

2. Accessing Elements in an Array of Arrays

To access elements inside an array of arrays, use bracket notation twice: the first bracket to access the nested array, and the second to access the element within that nested array.

Access a Nested Array:

let firstArray = arrayOfArrays[0]; // [1, 2, 3]
let secondArray = arrayOfArrays[1]; // [4, 5, 6]

Access an Element in a Nested Array:

let firstElement = arrayOfArrays[0][0]; // 1
let secondElement = arrayOfArrays[1][2]; // 6

3. Iterating Over an Array of Arrays

You can use nested loops to iterate over an array of arrays.

Using for Loop:

for (let i = 0; i < arrayOfArrays.length; i++) {
  for (let j = 0; j < arrayOfArrays[i].length; j++) {
    console.log(arrayOfArrays[i][j]);
  }
}

Using forEach Loop:

arrayOfArrays.forEach(innerArray => {
  innerArray.forEach(element => {
    console.log(element);
  });
});

4. Adding Elements to an Array of Arrays

You can add elements to both the main array and the nested arrays.

Add a New Nested Array:

arrayOfArrays.push([10, 11, 12]);
console.log(arrayOfArrays);
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

Add an Element to an Existing Nested Array:

arrayOfArrays[0].push(0);
console.log(arrayOfArrays[0]);
// Output: [1, 2, 3, 0]

5. Removing Elements from an Array of Arrays

You can remove elements using methods like pop, shift, or splice.

Remove a Nested Array:

let removedArray = arrayOfArrays.pop();
console.log(removedArray); // [10, 11, 12]
console.log(arrayOfArrays);
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Remove an Element from a Nested Array:

let removedElement = arrayOfArrays[0].pop();
console.log(removedElement); // 0
console.log(arrayOfArrays[0]);
// Output: [1, 2, 3]

Example Usage

Here is an example that demonstrates various operations on an array of arrays:

let arrayOfArrays = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

// Access elements
console.log(arrayOfArrays[1][1]); // 5

// Iterate over array of arrays
arrayOfArrays.forEach(innerArray => {
  innerArray.forEach(element => {
    console.log(element);
  });
});

// Add elements
arrayOfArrays.push([10, 11, 12]);
arrayOfArrays[0].push(0);

// Remove elements
arrayOfArrays.pop();
arrayOfArrays[0].pop();

console.log(arrayOfArrays);

Accessing and manipulating arrays inside an array in JavaScript is straightforward using bracket notation.

Also Read

How to Check If a Key Exists in a JavaScript Object

Understanding “this” in JavaScript

About Post Author