-1

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

  {
    "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

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
Kumara
  • 471
  • 2
  • 10
  • 33

2 Answers2

2

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))
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
0

You have to specify the correct path and iterate over the result array:

for (let i=0; i < abc.result.length; i++) {
    abc.result[i].select = false
} 
Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22