-2

I need to add key and its value in a existing json array using Javascript. 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.

 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. I have also tried the below code.

for(var key in finalOut){
     finalOut[key]['total_disposition_code'][key]=
    //console.log(finalOut[key]);
}
  • There's no [JSON](http://json.org) in your question. That's an array of objects -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jan 29 '19 at 06:43

2 Answers2

0

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;
  }
}) 
wentjun
  • 40,384
  • 10
  • 95
  • 107
0

ES6 you can use the code and it will return the object you liked

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