Lonely Lonely - 1 month ago 5
TypeScript Question

JSON to JSON Converting: Flattening

How can I get from

data1 = [
{ "label": "Some Label", "value": '018'},
{ "label": "Another Label", "value": '020'}
]


to

data2 = {
"018": "Some Label",
"020": "Another Label"
}


how should the for loop look like?

for (let item of data){
....
}

Answer

If you want to stick with the for..of loop, you can do:

var data2 = {};

for (let item of data) {
  data2[item.value] = item.label
}