在现有对象中添加 javascript 对象 [英] adding javascript object in existing object

查看:35
本文介绍了在现有对象中添加 javascript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我对 javascript 了解很多,但我必须承认我一无所知.我一直在尝试创建一个自定义对象,但似乎仍然无处可去.

var currentline=[{"NAME":"Battery For Alarm Panel","CODE":"POWER MAX","OWN":"ONM"},{"NAME":"火警面板","CODE":"SANA SERVICES","OWN":"ONM"}...]

通过以下问题的帮助,我能够创建以下对象

//为简洁起见跳过主循环代码detailObj=[];环形{singleObj = {};singleObj[currentline['NAME']] = {};singleObj[currentline['NAME']][currentline['CODE']] = {};singleObj[currentline['NAME']][currentline['CODE']][currentline['OWN']] = value;detailObj.push(singleObj);}

并获得以下 detailObj

<预><代码> [{"报警面板电池":{"POWER MAX":{"ONM":7}}},{"火警面板":{"SANA SERVICES":{"ONM":8}}}]

我怎样才能push一个对象到对象中,尽管它是数组,并且可以获得以下对象?

<代码> {"报警面板电池":{"POWER MAX":{"ONM":7}},"火警面板":{"SANA SERVICES":{"ONM":8}}}

有没有在对象中添加对象的函数?

解决方案

如果currentline是这样的

var currentline = [{"NAME": "报警面板电池","CODE": "最大功率","OWN": "ONM",值":7}, {"NAME": "火警面板","代码": "SANA 服务","OWN": "ONM",值":8}];

只需将 detailObj 更改为一个对象,而不是推送,而是将 NAMES 值分配为键,就像这样

var detailObj = {};//一个对象,而不是一个数组currentline.forEach(函数(行){detailObj[line.NAME] = {};detailObj[line.NAME][line.CODE] = {};detailObj[line.NAME][line.CODE][line.OWN] = line.VALUE;});控制台日志(detailObj);# { '报警面板电池': { 'POWER MAX': { ONM: 7 } },#'火警面板':{'SANA服务':{ONM:8}}}

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 } } }

这篇关于在现有对象中添加 javascript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆