I have the following JSON:
{
"temperature": "22.2",
"power": "6",
"current": "156"
}
and I need to convert it to this explicit structure:
{
"key": "temperature",
"value": "22.2"
},
{
"key": "power",
"value": "6"
},
{
"key": "current",
"value": "156"
}
Is there an elegant, simple and quick way to do this?
Best, thx
var newStructure = Object.keys(obj).map(function(key){
return {'key':key, 'value':obj[key]}
})
Example
var obj = { "temperature": "22.2", "power": "6", "current": "156" } var arr = Object.keys(obj).map(function(key){return {'key':key,'value':obj[key]}}) console.log(arr)
Object.hashToKeyValuePairs = function (hash) { var ret = []; for (var i in hash) ret.push({ key: i, value: hash[i]}); return ret; }; // example document.body.innerHTML = JSON.stringify( Object.hashToKeyValuePairs({ a: 1, b: 2, c: 3 }) );
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.