Sunday, May 19, 2024
96
rated 0 times [  98] [ 2]  / answers: 1 / hits: 17100  / 8 Years ago, wed, august 17, 2016, 12:00:00

to begin with, I have a multilevel of entities as in



country unit ----> customer reporting group ----> customers



each country unit has different customer reporting groups and each of the later has different customers



in the code the variable names are



cu ----> crg ---> customer



this is represented in a multilevel object called menuData:



menuData = {
cu1: {
CRG3: {
Customer1: {},
Customer5: {}
},
CRG7: {
Customer3: {},
Customer2: {},
Customer7: {}
}
},
cu4: {
CRG1: {
Customer2: {},
Customer4: {}
},
CRG3: {
Customer4: {}
}
}
};


what I wanted to do is to construct unique id for each level in a multilevel objects as well as in for example the ids for the customer units will be the same



cu1 and cu2 and so on



for the customer reporting groups the ids will consist of the cu + the crg as in
cu1+crg4



for the customer:



cu1+crg4+customer6;



what I did is a function called getIds



var getIds = function(menuData) {
var ids = {};
for (cu in menuData) {
ids[cu] = cu;
for (crg in menuData[cu]) {
if (!(ids[cu] in ids)) {
ids[cu] = {};
ids[cu][crg] = ids[cu].concat(crg);
} else ids[cu][crg] = ids[cu].concat(crg);
for (customer in menuData[cu][crg]) {
if (!ids[cu][crg]) {
ids[cu][crg] = {};
ids[cu][crg][customer] = ids[cu][crg].concat(customer);
} else ids[cu][crg][customer] = ids[cu][crg].concat(customer);
}
}
}
console.log(ids);
return ids;
};


the error I got is




Cannot read property 'concat' of undefined




what I have tried is that, because it says that it's undefined, I try to define it if its not already defined as in



if (!(ids[cu] in ids)) {
ids[cu] = {};
ids[cu][crg] = ids[cu].concat(crg);
}


if its not defined, define it and insert the value, but if its defined, only assign the value
else ids[cu][crg] = ids[cu].concat (crg );



why do I get this error? and how to get the the ids in multilevel objects ?






edit, excpected output is



ids = {
cu1: {
cu1+CRG3: { cu1+CRG3+Customer1:{}, cu1+CRG3+Customer5:{} },
cu1+CRG7: { cu1+CRG7+Customer3:{}, cu1+CRG7+Customer2:{}, cu1+CRG7+Customer7:{} }
},
cu4: {
cu4+CRG1: { cu4+CRG1+Customer2:{}, cu4+CRG1+Customer4:{} },
cu4+CRG3: { cu4+CRG3+Customer4:{}}
}
}

More From » javascript-objects

 Answers
41

The Problem with your Code is that you are using Objects to store your data and Objects don´t have the Method "concat" only Arrays have the "concat" Method. Your Object must look like these to work:


 menuData = [
"cu1": [
"CRG3": [ "Customer1":{}, "Customer5":{} ],
"CRG7": [ "Customer3":{}, "Customer2":{}, "Customer7":{} ]
],
"cu4": [
"CRG1": [ "Customer2":{}, "Customer4":{} ],
"CRG3": [ "Customer4":{}]
]
]

Here´s a reference : MDN Array.concat()


What can be confusing in JS is that an Object Property can be accessed like an Array.


Update after Expected Output was added:


okay than i think concat is not the right solution for your Problem.


Try it with something like this:




var ids = {};

var menuData = {
cu1: {
CRG3: {
Customer1: {},
Customer5: {}
},
CRG7: {
Customer3: {},
Customer2: {},
Customer7: {}
}
},
cu4: {
CRG1: {
Customer2: {},
Customer4: {}
},
CRG3: {
Customer4: {}
}
}
};

for (propKeyLevel1 in menuData){
ids[propKeyLevel1] = {};
var propLevel1 = ids[propKeyLevel1];
for(propKeyLevel2 in menuData[propKeyLevel1]){
propLevel1[propKeyLevel1+++propKeyLevel2] = {};
var propLevel2 = propLevel1[propKeyLevel1+++propKeyLevel2];

for(propKeyLevel3 in menuData[propKeyLevel1][propKeyLevel2]){
propLevel2[propKeyLevel1+++propKeyLevel2+++propKeyLevel3] = {};
}
}

}

console.log(ids);




[#61011] Sunday, August 14, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruz

Total Points: 415
Total Questions: 98
Total Answers: 109

Location: France
Member since Thu, May 6, 2021
3 Years ago
;