Iteratively creating objects in an object

Hi,
I have an object called checkboxesState and it looks like this,

Object { AS: true, A2: true, GCSE: false, AP: false, KET: false, PET: true, FCE: true, IELTS: false }

I am trying to iterate through this object using each key as a key is a new object called ‘pos’, where these new keys each have a value for ‘r’ (for row) and ‘c’ (for column).

So the object ‘pos’ has objects in it with the same name as the keys in the above object. The ‘r’ and ‘c’ are variously assigned value like this.

for(var prop in checkboxesState){
        if(checkboxesState[prop])//generates position object entries for examTypes
        {
        console.log("exam " + prop + " checkboxesState " + checkboxesState[prop]);
                pos = {prop: {r:i,c:j}};
                /*var pos = new Object()
                pos.prop = i;
                prop:c = j;*/
                if(j%3 == 2)
                {
                    i++;
                    j=0;
                } else {
                    j++;
                }
        }
    }

The commented lines and the line directly above them, is where I know that I am going wrong!

I know that I am not creating the object ‘pos’ correctly or the objects within. I should end up with something like this,

pos { AS: {r: 0, c: 0}, A2: {r: 0, c: 1, PET: {r: 0, c: 2, FCE: {r: 1, c: 0} }

‘r’ and ‘c’ values are only added for exam types that are true in the original object checkboxes.

Thanks

Initialize your pos object outside of the loop, and use the array syntax to add an object as a new property:

var pos = {}, i = 0, j = 0;
for(var prop in checkboxesState) {
    if(checkboxesState[prop]) {
        pos[prop] = {r:i,c:j};
        if (j%3 == 2) {
            i++;
            j=0;
        } else {
            j++;
        }
    }
}
1 Like

Yes this exactly it, great thank you.

How can I return this object ‘pos’ from a function so that i can use it anywhere?

function position(){
                        var pos = {}, i = 0, j = 0;
                                for(var prop in checkboxesState){
                                    if(checkboxesState[prop] && checkboxesExist[prop])
                                        {
                                                pos[prop] = {r:i,c:j};
                                                if(j%3 == 2)
                                                {
                                                    i++;
                                                    j=0;
                                                } else {
                                                    j++;
                                                }
                                        }
                                    }
                                    return pos;
                                }

And then be able to use it,

   console.log(pos[exam].c);

I think what I am trying to say is that i would like the scope of this object to be global.

Thanks

There’s another problem that you’re going to have, and that is the properties of an object can not be guaranteed to always be processed in the same order.

Thanks I didn’t know that. But is this case it won’t matter because the r and c are row and column coordinates for a table.
I am not sure that the order of the properties will be an issue here, I will just call whatever property I need.
thanks

Actually this is so simple I don’t know what was wrong with me yesterday.

To call the values in the object all I need to do is,

x = position();
console.log(x[exam].c);

function position(){
                        var pos = {}, i = 0, j = 0;
                                for(var prop in checkboxesState){
                                    if(checkboxesState[prop] && checkboxesExist[prop])
                                        {
                                                pos[prop] = {r:i,c:j};
                                                if(j%3 == 2)
                                                {
                                                    i++;
                                                    j=0;
                                                } else {
                                                    j++;
                                                }
                                        }
                                    }
                                    return pos;
                                }

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.