I have this data in var output (this is just an excerpt):
{
'us-tx-021': 5,
'us-tx-029': 3,
'us-tx-031': 1,
}
I need to convert it to something like this:
[
{
'hc-key': "us-tx-021",
'value': 5
},
{
'hc-key': "us-tx-029",
'value': 3
},
{
'hc-key': "us-tx-031",
'value': 1
}
]
I don't know how to use the keys & values from one object as values for another object. Can anyone help me?
You can use a for
loop to get all keys from original json
var json = '{ "us-tx-0212": 5, "us-tx-029": 3, "us-tx-031": 1}';
var obj = JSON.parse(json);
var translatedObj = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
translatedObj.push({'key': key, 'value': obj[key] });
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.