You can do something like this:
var cubes = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
for(var i = 0; i < cubes.length; i++) {
var cube = cubes[i];
for(var j = 0; j < cube.length; j++) {
display("cube[" + i + "][" + j + "] = " + cube[j]);
}
}
Working jsFiddle:
- http://jsfiddle.net/TRR4n/
The output of the above:
cube[0][0] = 1
cube[0][1] = 2
cube[0][2] = 3
cube[1][0] = 4
cube[1][1] = 5
cube[1][2] = 6
cube[2][0] = 7
cube[2][1] = 8
cube[2][2] = 9
Answer from icyrock.com on Stack OverflowYou can do something like this:
var cubes = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
for(var i = 0; i < cubes.length; i++) {
var cube = cubes[i];
for(var j = 0; j < cube.length; j++) {
display("cube[" + i + "][" + j + "] = " + cube[j]);
}
}
Working jsFiddle:
- http://jsfiddle.net/TRR4n/
The output of the above:
cube[0][0] = 1
cube[0][1] = 2
cube[0][2] = 3
cube[1][0] = 4
cube[1][1] = 5
cube[1][2] = 6
cube[2][0] = 7
cube[2][1] = 8
cube[2][2] = 9
var cubes = [["string", "string"], ["string", "string"]];
for(var i = 0; i < cubes.length; i++) {
for(var j = 0; j < cubes[i].length; j++) {
console.log(cubes[i][j]);
}
}
Looping Through Multidimensional Array with Nested Arrays - JavaScript - SitePoint Forums | Web Development & Design Community
How to loop through & display Javascript Multidimensional Array - Stack Overflow
Iterate through multidimensional arrays using for loop
javascript - Loop through multidimensional array - Stack Overflow
Videos
To return a value in a multidimensional array: array[n][m] is the mth element of the nth row. To do this for every element, use embedded for-loops:
for (var i = 0; i < array.length; i++){
for (var j = 0; j < array[i].length; j++){
console.log(array[i][j]);
Using jQuery - but easily adaptable to pure JS:
var my_multi_dimensional_array = ["a", "b", "c", ["d", "e", "f"], "g", ["h", ["i"],
["j", "k"], "l", ["m", ["n", "o", [
[
["p"], "q"]
]]]
]];
(function walkies(arr, path, callback, root) {
$.each(arr, function (i, v) {
//push index onto path stack
path.push(i);
var recurseable = ($.isArray(v) || $.isPlainObject(v)) ;
var recurse = callback.call(v, !recurseable, v, path, i, root || arr) && recurseable;
//call callback and continue recursing this node until callback returns false
if ( recurse) {
walkies(v, path, callback, root || arr);
}
//pop last index off path stack
path.pop();
});
}(my_multi_dimensional_array, [], function (isLeaf, node, path, index, root) {
if( isLeaf ){
console.log( "[" + path.join("],[") + "]=" + node );
}
return true;
}));
jsFiddle
First, you should not add elements to arrays by key, but to objects. Which means your global object should be build as :
var _cQueue = [];
var valueToPush = {}; // this isn't an array but a js object used as map
valueToPush['[email protected]'] = '1234567';
_cQueue.push(valueToPush);
Then, you iterate using two kinds of loops :
for (var i=0; i<_cQueue.length; i++) { // iterate on the array
var obj = _cQueue[i];
for (var key in obj) { // iterate on object properties
var value = obj[key];
console.log(value);
}
}
See MDN's excellent Working with objects.
If you want to find the email associated to an id, you can do two things :
1) loop until you find it :
function find(id) {
for (var i=0; i<_cQueue.length; i++) { // iterate on the array
var obj = _cQueue[i];
for (var key in obj) { // iterate on object properties
var value = obj[key];
if (value==id) return key;
}
}
}
2) put all the ids in a map so that it can be found faster :
var bigMap = {};
for (var i=0; i<_cQueue.length; i++) { // iterate on the array
var obj = _cQueue[i];
for (var key in obj) { // iterate on object properties
bigMap[obj[key]] = key; // maps the id to the email
}
}
function find(id) {
return bigMap[id];
}
use for-in to both levels:
for(var val in _cQueue){
var obj = _cQueue[val];
for(var val1 in obj){
alert('key(email):' + val1 + '\nValue:' + obj[val1]);
}
}
With due credit to @Matt who's comment points it out.
The commonest format for looping through an array is:
for(var i=0; i<array.length; i++) {
doSomethingWith(array[i];
}
Note that's a "less than" operator, not a "less than or equal to" operator.
This loop counts from 0 to array.length - 1, because the second part of the for statement: i < array.length, means "keep repeating for as long as i is less than array.length.
... and that's what you want, because arrays are numbered from 0 to length-1. That is, an array of length 4 is numbered 0,1,2,3.
If you loop while i <= 4, then the loop will execute for 0,1,2,3,4 -- and in Javascript, it will get undefined when it references array[4].
Sometimes you do need "<=" in a for loop, but it's very much the exception. Any time you do use "<=", think about adding a comment to say why.
Your problem is your index (out of bounds) when i=acKey.length.
You can use i<acKey.lenght or implement a "for each" iteration to avoid confusion:
acKey = [
["keydown", "alt+d", open],
["keydown", "alt+a", close],
["keydown", "alt+s", max],
];
var sub;
for(i in acKey) {
sub = acKey[i];
$(document).bind(sub[0], sub[1], sub[2]);
}
This code should work:
var cubes = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
for(var i=0; i<cubes[0].length; ++i)
for(var j=0; j<cubes[1].length; ++j)
for(var k=0; k<cubes[2].length; ++k) {
alert([cubes[0][i],cubes[1][j],cubes[2][k]]);
}
This works for the given array, but means if you have more than three inner arrays, or more array dimensions then you would have to manually edit the javascript.
$(document).ready(function(){
var cubes = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
var output = "";
for(var a = 0; a < cubes[0].length; a++)
for(var b = 0; b < cubes[1].length; b++)
for(var c = 0; c < cubes[2].length; c++) {
output = output + [cubes[0][a],cubes[1][b],cubes[2][c]] + ",<br />";
}
$('#output').html(output);
});
Working example: http://jsfiddle.net/HtSkd/