Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:14:18+00:00 2026-06-14T18:14:18+00:00

I have a number of Javascript objects. I want a simple ‘for loop’ (in

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:14:19+00:00Added an answer on June 14, 2026 at 6:14 pm
    // 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]);
             }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large number of objects that I want to declare. However,
Possible Duplicate: JavaScript: formatting number with exactly two decimals Now that I have got
I have a javascript function that accepts a number and performs a mathematical operation
I have a basic dataset stored in javascript that I want to filter based
I have a javascript object, which I've added a number of prototyped functions to,
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a hexadecimal number in javascript. For display purposes, I would like to
I have a JavaScript function which is quite long and performs a number of
I'm progamming in javascript/jQuery. Say, I have an array with includes a few number
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.