1

I have multiple objects like the one below, and I was wondering what the correct syntax would be for putting them all within a single array. I'm also wondering how to correctly cycle through all of the arrays.


var verbMap = [
    {
        infinitive: "gehen",
        thirdPres: "geht",
        thirdPast: "ging",
        aux: "ist",
        pastPart: "gegangen",
        english: "go"
    },
    {
        infinitive: "gelingen",
        thirdPres: "gelingt",
        thirdPast: "gelang",
        aux: "ist",
        pastPart: "gelungen",
        english: "succeed"
    }
];

I know the correct way to cycle through that above array is:


for(v in verbMap){
    for(p in verbMap[v]){
    }
}

If I wanted to cycle through a larger array holding multiple arrays like verbMap, what would be the correct way to do that?

benekastah
  • 5,651
  • 1
  • 35
  • 50
ayyp
  • 6,590
  • 4
  • 33
  • 47
  • 3
    Note that those inner "arrays" are in fact objects (`{}`), not arrays (`[]`). – pimvdb Oct 05 '11 at 15:07
  • In JavaScript, everything is an object ;-) – Zach Lysobey Oct 05 '11 at 15:08
  • 1
    @Zach L.: Yes, but not everything is an array :) – pimvdb Oct 05 '11 at 15:08
  • 1
    Use a normal `for` loop for arrays, use `for...in` for objects. – Felix Kling Oct 05 '11 at 15:08
  • When first learning javascript I also confused the plain object `{}` to be an associative array (like in php, for example) because you can use the same syntax: `someObject["item"]`. Instead of thinking of this as an associative array, think of it as a hashtable. It makes a lot more sense since arrays are ordered and object properties are not. – benekastah Oct 05 '11 at 15:17

5 Answers5

4

Just put the verbMap arrays in another array.

var verbMaps = [verbMap1, verbMap2...]

The key thing to understand is that your verbMap is an array of object literals. Only use

for (k in verbMap)...

for object literals.

The correct way to loop thru an array is something like

for (var i = 0; i < verbMaps.length; i++) {
    var currentVerbMap = verbMaps[i];
    for (var j = 0; j < currentVerbMap.length; j++) {
        var currentHash = currentVerbMap[j];
        for (var k in currentHash) {
           console.log(k, currentHash[k];
        }
    }
}
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • If I wanted to write that out a table, like this: http://jsfiddle.net/Skooljester/wANRp/ do you know how I would do that? – ayyp Oct 05 '11 at 16:19
  • 1
    @Andrew start by changing the outer loop to use `var i;...` as outlined in my answer. – hvgotcodes Oct 05 '11 at 17:38
1

The following function outputs every value from a (possibly) infinite array given as a parameter.

function printInfiniteArray(value){
    if (value instanceof Array){
        for(i=0;i<value.length;i++){
            printInfiniteArray(value[i]);
        }
    } else {
        console.log(value);
    }
}

Edited code. Thanks jtfairbank

Matyas
  • 13,473
  • 3
  • 60
  • 73
  • Do not use for...in with arrays in javascript. http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays – jtfairbank Oct 05 '11 at 15:15
0

Your array does not contain other arrays. It contains objects. You could try this to loop though it.

for(var i = 0; i < verbMap.length; i++)
{
   var obj = verbMap[i];
   alert("Object #"+ i " - infinitive: " + obj.infinitive);
}
brenjt
  • 15,997
  • 13
  • 77
  • 118
0

You would treat the array like any other javascript object.

var arrayOfArrays = [];
var array1 = ["cows", "horses", "chicken"];
var array2 = ["moo", "neigh", "cock-adoodle-doo"];

arrayOfArrays[0] = array1;
arrayOfArrays[1] = array2;

You can also use javascript's literal notation to create a multi-dimentional array:

var arrayOfArrays = [ ["meat", "veggies"], ["mmmm!", "yuck!"] ];

To cycle through the array of arrays, you'll need to use nested for loops, like so:

for (var i = 0; i < arrayOfArrays.length; i++) {
    var myArray = arrayOfArrays[i];
    for (var j = 0; j < myArray.length; j++) {
        var myData = myArray[0]; // = arrayOfArrays[0][0];
    }
}

DO NOT USE For...in!!! That is not what it was made for. In javascript, For...in can have some unwanted behaviors. See Why is using "for...in" with array iteration a bad idea? for more detail.

Community
  • 1
  • 1
jtfairbank
  • 2,311
  • 2
  • 21
  • 33
0

You can use jQuery.each to cycle through an array or object, without having to check which one it is. A simple recursive function to cycle through key-value pairs in a nested structure, without knowing the exact depth:

var walk = function(o) {
    $.each(o, function(key, value) {
        if (typeof value == 'object') {
            walk(value);
        } else {
            console.log(key, value);
        }
    });
}
Tgr
  • 27,442
  • 12
  • 81
  • 118