In previous tutorials we’ve taken a look at JavaScript array basics, manipulating arrays, and sorting arrays. So far, all the arrays we’ve dealt with have been “flat” arrays; each array element contains a single value, such as a number, string, or object.
However, like most programming languages, JavaScript lets you create arrays inside arrays, known as nested arrays. In a nested array, the elements of one array are themselves arrays. For example:
var pets = new Array ( );
pets[0] = new Array ( "Sheba", 13, "cat" );
pets[1] = new Array ( "Jasper", 12, "dog" );
alert ( pets[0][0] + " is a " + pets[0][1] + " year old " + pets[0][2] ); // Displays "Sheba is a 13 year old cat"
alert ( pets[1][0] + " is a " + pets[1][1] + " year old " + pets[1][2] ); // Displays "Jasper is a 12 year old dog"
Here we’ve created an array of 2 elements. Each element is in turn an array containing 3 elements. To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2]
accesses the 3rd element of the array inside the 2nd element of the pets
array.
You can nest arrays as deeply as you like. For example, here we’ve created a top-level array called animals
, into which we’ve placed the above pets
array, as well as a similar dinosaurs
array — that’s 3 levels of nesting in total:
var pets = new Array ( );
pets[0] = new Array ( "Sheba", 13, "cat" );
pets[1] = new Array ( "Jasper", 12, "dog" );
var dinosaurs = new Array ( );
dinosaurs[0] = new Array ( "Cyril", 45, "Tyrannosaur" );
dinosaurs[1] = new Array ( "Gertrude", 72, "Brontosaur" );
var animals = new Array ( pets, dinosaurs );
alert ( animals[0][1][0] + " is a " + animals[0][1][1] + " year old " + animals[0][1][2] ); // Displays "Jasper is a 12 year old dog"
Looping through nested arrays
Of course, once you start nesting arrays you quickly end up with lots of array elements. The easiest way to work with large arrays is to use loops. And in order to process nested arrays, you need to use nested loops.
For example, the following code loops through the animals
nested array we created above, displaying each of the animals, along with their ages and types:
for ( i=0; i<animals.length; i++ )
{
for ( j=0; j<animals[i].length; j++ )
{
alert ( animals[i][j][0] + " is a " + animals[i][j][1] + " year old " + animals[i][j][2] );
}
}
You now know how to create and use nested arrays in JavaScript. You’ll find nested arrays are useful when you want to store highly structured data — such as our animals
example above — and when you’re creating multidimensional data structures (commonly used in games and graphics applications). Enjoy!
gummie says
Thank you.
Isaac says
Mr Matt you only put down the solution for your nested loop.. can u break it down in detail so some of us can really understand and apply .. thanks alot.
toni says
Hi!
Imagine something like:
const newItem = [‘item 1’, ‘item 0’, ‘item 1’, ‘item 2’, ‘item 1’, ‘item 0’];
If I want to remove all ‘item 1’ I can use:
for(let i = newItem.length-1; i–;){
if (newItem[i] === “item 1”) newItem.splice(i, 1);
}
The question is if I have an array inside another array how can I do?
const newItem = [
[‘item 1’, ‘item 0’, ‘item 1’],
[‘item 2’, ‘item 1’, ‘item 0’]
];
Thanks
Matt Doyle says
Nested loops should do the trick. Place a for loop inside another for loop. The outer loop iterates though the items in the outer array; the inner loop iterates through the items in the inner arrays.
ProDevs says
@Toni,
You may want to use nested loop like,
Quicky says
Hi Mat, I got a trick problem.
I extract from two differen Databases in different environments Data to arrays.
I merge this two arrays an get a wrong json .
The content ist there as
If I read the result
Same problem I have when I get the result in TWO Arrays like
Matt Doyle says
Can you post the full code on something like https://codesandbox.io so I can see the problem?
Quicky says
Hi Matt,
I load a zip with all informatios on my server.
you can download it here.
http://dmdg.io/dmdg.zip
Hope the errordescription gave you all information yo need.
Kind regards Gert
Rupesh patil says
How i can print all the elements array.
For example:
Input:
Let arr= [1,2,[3,4,[5,6,7]]];
Output : 1234567
Velusamy says
[1,2,[3,4,[5,6,7]]].flat(Infinity).join(“”); // 1234567
muhammad shahroz says
var = arr [[{},{},{}],[{},{},{}],[{},{},{}],[{},{},{}],[{},{},{}],];
how to access inside of obj name etc
Her says
Very helpful. Thank you!
Ricardo Leme Silva says
I could kiss you!! LOL. A whole day to discover you…
varshitha says
I have a question
EmpList[{“key”:”value”, “key1″:”value1”},{“firstname”:”lastname”}];
How do I get details of value and lastname.
manoj kumar says
use this arrayname[index].propertyname. Let’s say you want to get the value of key and firstname.
so, EmpList[0].value.
you will get value.