0

I have a number of Javascript objects.

I want a simple 'for loop' (in JS) that prints out the key:value pairs within each object.

I've shown what I've done below (and here it is in a FIDDLE), but the variable 'thisvar' isn't working.

I'm obviously missing something really simple - can you point out what it is?

Thanks

// declare objects
var variant1 = {};
var variant2 = {};

variant1['a'] = 'apple';
variant1['b'] = 'orange';
variant1['c'] = 'pear';

variant2['a'] = 'red';
variant2['b'] = 'green';
variant2['c'] = 'blue';

// run through each object
for (i=1; i<3; i++){
    var thisvar = variant+i;
    for(var newindex in thisvar) {
        var name = newindex;
        var contents = thisvar[newindex];
        alert(name+'='+contents);
    }
}

1
  • var thisvar = variant+i; you cannot create dynamic variables like that. Commented Nov 23, 2012 at 9:22

3 Answers 3

3
// declare objects
var container = {
    variant1: {}
    variant2: {}
}

container.variant1['a'] = 'apple';
container.variant1['b'] = 'orange';
container.variant1['c'] = 'pear';

container.variant2['a'] = 'red';
container.variant2['b'] = 'green';
container.variant2['c'] = 'blue';

// run through each object
for (var i = 1; i < 3; i++){
    for (prop in container["variant"+i]){
         if (variant.hasOwnProperty(prop)){
            alert(prop+'='+var[prop]);
         }
    }
}

As pointed out in the comments below, if you want to numerically iterate over the properties, you're better off just using an array:

// declare objects
var variants = [{},{}];

variants[0]['a'] = 'apple';
variants[0]['b'] = 'orange';
variants[0]['c'] = 'pear';

variants[1]['a'] = 'red';
variants[1]['b'] = 'green';
variants[1]['c'] = 'blue';

// run through each object
for (var i = 0; i < 2; i++){
    for (prop in variants[i]){
         if (variant.hasOwnProperty(prop)){
            alert(prop+'='+var[prop]);
         }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Only works with global variables, just sayin'. OP is better off using an array of objects.
@FelixKling I'm assuming you meant object map of objects. Fixed my answer.
No, I actually really meant array of objects... the "variantX" keys just seem to be a numeration, which arrays provide natively (kind of). But then again, there are multiple solutions for a problem :)
But I'd read in many places "Don't store key-value pairs in Arrays" (in objects instead) - phabricator.com/docs/phabricator/article/…
@Steve: Well, not string keys, but numerical, consecutive keys (indexes) are fine. That's what makes an array an array.
1

Try this:

var variant1 = new Object();
variant1['a'] = 'apple';
variant1['b'] = 'orange';
variant1['c'] = 'pear';
var variant2 = new Object();
variant2['a'] = 'red';
variant2['b'] = 'green';
variant2['c'] = 'blue';


for (i=1; i<3; i++){
var thisvar ;
eval( 'thisvar = variant'+i);
    for(var newindex in thisvar) {
        var name = newindex;
        var contents = thisvar[newindex];
        alert(name+'='+contents);
    }
}

1 Comment

Please avoid the use of eval.
1

If you just want to inspect the object properties you can simply do

alert(JSON.stringify(container))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.