将JSON对象的表示形式更改为显式键/值格式(Change representation of JSON object to explicit key/value format)
我有以下JSON:
{ "temperature": "22.2", "power": "6", "current": "156" }我需要将它转换为这个显式结构:
{ "key": "temperature", "value": "22.2" }, { "key": "power", "value": "6" }, { "key": "current", "value": "156" }这是一种优雅,简单,快捷的方法吗?
最好的,thx
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]} })例
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) 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)
更多推荐
发布评论