简体   繁体   English

如何使用javascript在现有的json数组中添加键及其值

[英]How to add key and its value in existing json array using javascript

I need to add key and its value in a existing json array using Javascript.我需要使用 Javascript 在现有的 json 数组中添加键及其值。 I am explaining my array below.我在下面解释我的数组。

var finalOut=[
            {
                "location": "Jaipur",
                "nos_of_fos": 4,
                "login_id": [
                    "9619300332",
                    "9619300347"
                ],
                "total_remarks": 0,
                "total_disposition_code": {"PTP": 3,
                    "CB": 1,"TPT":4}
            },
            {
                "location": "BHILWARA,",
                "nos_of_fos": 1,
                "login_id": [
                    "9619300346"
                ],
                "total_remarks": 4,
                "total_disposition_code": {
                    "PTP": 3,
                    "CB": 1
                }
            },
            {
                "location": "AJMER",
                "nos_of_fos": 2,
                "login_id": [
                    "9619300357"
                ],
                "total_remarks": 0,
                "total_disposition_code": {}
            }
        ]

Here I need if total_disposition_code has any value then its all key and value will add in root object and the expected output result is given below.这里我需要如果total_disposition_code有任何值,那么它的所有键和值都将添加到根对象中,并且下面给出了预期的输出结果。

 var result=[
                {
                    "location": "Jaipur",
                    "nos_of_fos": 4,
                    "login_id": [
                        "9619300332",
                        "9619300347"
                    ],
                    "total_remarks": 0,
                    "PTP":3,
                    "CB":1,
                    "TPT":4
                },
                {
                    "location": "BHILWARA,",
                    "nos_of_fos": 1,
                    "login_id": [
                        "9619300346"
                    ],
                    "total_remarks": 4,
                    "PTP":3,
                    "CB":1,
                },
                {
                    "location": "AJMER",
                    "nos_of_fos": 2,
                    "login_id": [
                        "9619300357"
                    ],
                    "total_remarks": 0,
                }
            ]

I need the above format using javascript.我需要使用 javascript 的上述格式。 I have also tried the below code.我也试过下面的代码。

for(var key in finalOut){
     finalOut[key]['total_disposition_code'][key]=
    //console.log(finalOut[key]);
}

You can simply use the spread syntax, and do something like this:您可以简单地使用传播语法,并执行以下操作:

const result = finalOut.map(object => {
  if (object['total_disposition_code']) {
    const mergedObject = {...object, ...object['total_disposition_code']}
    delete mergedObject['total_disposition_code']
    return mergedObject;
  }
}) 

ES6 你可以使用代码,它会返回你喜欢的对象

for(let i = 0; i<finalOut.length; i++) { a[i] = {...a[i].total_disposition_code, ...a[i]}; delete a[i].total_disposition_code; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM