Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add new attribute for the JSON Array List

I need to add new attribute for JSON object. currently my code like as below

 let abc  =  this.dataSource.data;
            console.log(abc);

console log data set like below

{
    "result": [
        {
            "id": 210,
            "temp": "210",
            "city": "colombo",
            "status": "waiting"
        },
        {
             "id": 211,
            "temp": "212",
            "city": "colombo2",
            "status": "waiting"
        },
    ]
}

I need to add new attribute for the JSON object. My expect JSON object like below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  {
    "result": [
        {
            "id": 210,
            "temp": "210",
            "city": "colombo",
            "status": "waiting"
        "select": false,
        },
        {
             "id": 211,
            "temp": "212",
            "city": "colombo2",
            "status": "waiting",
            "select": false,
        },
    ]
  }

I tried to do it using this,

abc = {...abc,select:false}

but it did not work. How I do this

>Solution :

Just use map:

const data = {
"result": [
    {
        "id": 210,
        "temp": "210",
        "city": "colombo",
        "status": "waiting"
    },
    {
         "id": 211,
        "temp": "212",
        "city": "colombo2",
        "status": "waiting"
    },
]
}
  
  function formatData(data){
    return data.result.map(res=> ({...res, select: false}))
  }

console.log(formatData(data))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading