Multiple Dynamic Object Properties

130 views Asked by At

i'm wondering if its possible to create multiple Properties of an Object inside a for loop.

im trying it like this, but it isn't working correct:

var dataObjekt= {}; 
for(var i=0;i<req.session.dataObjekt.length;i++) {
 for(var v=0;v<req.session.dataObjekt[i].data.length;v++) { 
   dataObjekt[req.session.userObjekt[i].User] = {
     [req.session.dataObjekt[i].data[v]]: {
     },
   };
 }

}

i want to get:

User 1 { data 1: { }, data2: { }, data3: { }, ....};
User 2 { data 1: { }, data2: { }, data3: { }, ....};
User 3 { data 1: { }, data2: { }, data3: { }, ....};

but what i get is:

User 1 { last data: { };
User 2 { last data: { };
User 3 { last data: { };

the Objekt only "saves" the last Element of my (v) for loop. what ever im trying to do, i can't get it to work.

thanks in advance.

1

There are 1 answers

1
Sanjucta On BEST ANSWER

What seems to be happening is that you are overwriting dataObjekt[req.session.userObjekt[i].User] in every iteration of the inner loop.

You can try this :

    var dataObjekt= {}; 
    for(var i=0;i<req.session.dataObjekt.length;i++) {
         dataObjekt[req.session.userObjekt[i].User] = [];
         for(var v=0;v<req.session.dataObjekt[i].data.length;v++) { 
           dataObjekt[req.session.userObjekt[i].User].push({
             [req.session.dataObjekt[i].data[v]]: {
       });
    };
   }