I was thinking that i know much about javascript
but i must have to admit i know nothing. i have been trying to create a custom object but still seems no where.
var currentline=[
{"NAME":"Battery For Alarm Panel","CODE":"POWER MAX","OWN":"ONM"},
{"NAME":"Fire Alarm Panel","CODE":"SANA SERVICES","OWN":"ONM"}...
]
Through help of following question i'm able to create following object
// skipping main loop code for brevity
detailObj=[];
loop{
singleObj = {};
singleObj[currentline['NAME']] = {};
singleObj[currentline['NAME']][currentline['CODE']] = {};
singleObj[currentline['NAME']][currentline['CODE']][currentline['OWN']] = value;
detailObj.push(singleObj);
}
AND get the following detailObj
[
{"Battery For Alarm Panel":{"POWER MAX":{"ONM":7}}},
{"Fire Alarm Panel":{"SANA SERVICES":{"ONM":8}}}
]
how i can push
an object into object despite into array and could get following object?
{
"Battery For Alarm Panel":{"POWER MAX":{"ONM":7}},
"Fire Alarm Panel":{"SANA SERVICES":{"ONM":8}}
}
is there any function to add object in the object?
If currentline
is like this
var currentline = [{
"NAME": "Battery For Alarm Panel",
"CODE": "POWER MAX",
"OWN": "ONM",
"VALUE": 7
}, {
"NAME": "Fire Alarm Panel",
"CODE": "SANA SERVICES",
"OWN": "ONM",
"VALUE": 8
}];
Simply change the detailObj
an object and instead of pushing, assign NAMES
values as keys, like this
var detailObj = {}; // An object, not an array
currentline.forEach(function(line) {
detailObj[line.NAME] = {};
detailObj[line.NAME][line.CODE] = {};
detailObj[line.NAME][line.CODE][line.OWN] = line.VALUE;
});
console.log(detailObj);
# { 'Battery For Alarm Panel': { 'POWER MAX': { ONM: 7 } },
# 'Fire Alarm Panel': { 'SANA SERVICES': { ONM: 8 } } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.