0

I am trying to create a loop or a nested loop that will create one(1) array containing many objects:

// example of the object structure
obj0.country = distAttr[0];
obj0[municipo[0]] = econ[0];
obj0[municipo[1]] = edu[0];
obj0[municipo[2]] = gov[0];
obj0[municipo[3]] = health[0];
obj0[municipo[4]] = infra[0];
obj0[municipo[5]] = social[0];

obj1.country = distAttr[1];
obj1[municipo[0]] = econ[1];
obj1[municipo[1]] = edu[1];
obj1[municipo[2]] = gov[1];
obj1[municipo[3]] = health[1];
obj1[municipo[4]] = infra[1];
obj1[municipo[5]] = social[1];

// ... obj18

This is what i have so far:

// create all the objects, distAttr length is 19
for (var i = 0; i < distAttr.length; i++) {
    window['obj'+i ] = {};
};

// distName length is 6
var number = distName.length;

// this loop I can't figure out
for (var j = 0; i < distName.length; j++) {
    window['obj'+i ][municipo[j]] = econ[i];
};

// bind the objects to the array
for (var i = 0; i < distAttr.length; i++) {
    chartArray[i] = window['obj'+i];
};
3
  • But what's the problem you're facing? Commented Dec 13, 2013 at 18:03
  • I don't see a nested loop here. Commented Dec 13, 2013 at 18:04
  • I can not figure out how to build multiple objects that have the same structure as the above example. window['obj'+i ][municipo[j]] = econ[i]; does not create the objects. Commented Dec 13, 2013 at 18:06

1 Answer 1

1

You can build the object within a single loop:

// Set up some variables and the field values you will use:
var j,
    obj,
    ec = municipo[0],
    ed = municipo[1],
    go = municipo[2],
    he = municipo[3],
    in = municipo[4],
    so = municipo[5];

// Loop through the array.
for (i = 0; i < distAttr.length; i++) {
    // Create an object with a country field. 
    obj = { country: distAttr[i] };
    // Populate the other fields.
    obj[ec] = econ[i];
    obj[ed] = edu[i];
    obj[go] = gov[i];
    obj[he] = health[i];
    obj[in] = infra[i];
    obj[so] = social[i];
    // Set the array index to contain the object
    // (and if you need it then create a global object `objx`
    //  - not sure if you need it though.)
    chartArray[i] = window['obj'+i] = obj;
};
Sign up to request clarification or add additional context in comments.

4 Comments

obj will reference a new object at each iteration but at the end of the iteration obj is assigned to be an element of chartArray so the references maintained in that array are not lost or overwritten. You are going to end up with an array of multiple different objects (rather than overwriting the object multiple times).
The reason that obj is declared as a variable outside the scope of the loop is so that JavaScript does not try to create a new variable (and allocate new memory) with each iteration.
I'm not sure, but I think the engine create a new variable, but kills the old one.
It will create an {} object with each iteration but does not need to re-create obj (as a reference to the {}) with each iteration and does not need to garbage collect obj (as it will be re-used for subsequent iterations). If you declare var obj inside the loop then it may need to create both with each iteration and may also need to garbage collect the obj reference for each iteration (depending on how it can optimize the block).

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.