how to loop through multidimensional array in javascript code example
Example 1: nested array loop in javascript
let chunked = [[1,2,3], [4,5,6], [7,8,9]];
for(let i = 0; i < chunked.length; i++) {
for(let j = 0; j < chunked[i].length; j++) {
console.log(chunked[i][j]);
}
}
Example 2: javascript loop over three-dimensional array
var items = [[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]];
var result = [];
items.forEach(function(item)
{
var obj = {};
item.forEach(function (value)
{
obj[value[0]] = value[1];
});
result.push(obj);
});
console.log(result);
Example 3: create multidimensional array javascript for loop
var squares = new Array();
for(var i = 0; i <= 8; i++)
{
squares[i] = new Array();
for(var j = (i * 20) + 1; j <= 20 * i + 20; j++)
if (squares[i] == null)
squares[i] = j;
else
squares[i].push(j);
}