I am looping over some object and updating the values of another based on some lookups, currently I have problems with getting the correct values in the mapping.
let users = [
{
"id": "123",
"email": "[email protected]",
"name": " John Doe",
"group": "1",
"subgroup": "1"
},
{
"id": "456",
"email": "[email protected]",
"name": " Tom hans",
"group": "2",
"subgroup": "1"
}
];
let groupData = [
{
id: "1",
name: "Stars",
subgroup: [
{
id: "1",
pseudo: "big"
},
{
id: "2",
pseudo: "small"
}
]
},
{
id: "2",
name: "Stones",
subgroup: [
{
id: "1",
pseudo: "tiny"
},
{
id: "2",
pseudo: "huge"
}
]
}
];
const allUsers = {};
for (var i = 0; i < users.length; i++) {
let groupID = users[i].group,
subgroupID = users[i].subgroup;
users[i].group = users[i].group ? groupData[groupID][name] : '';
users[i].subgroup = users[i].subgroup ?
groupData[groupID].subgroup[subgroupID][name] : '';
allUsers[users[i].id] = users[i];
}
The result of allUsers will look like:
{
123: {
"id": "123",
"email": "[email protected]",
"name": " John Doe",
"group": "Stars",
"subgroup": "big"
},
456: {
"id": "456",
"email": "[email protected]",
"name": " Tom hans",
"group": "Stones",
"subgroup": "tiny"
}
};
Currently, I have lookup problems inside my for loop to get the name of the group/subgroup.
Could somebody help to fix this issue? preferably with ecma6.