i have a jsonStrng like var sourceJsonStr= {"foo":25,"xyz":49};
I want similar in JSON
object like var targetStrJson = [['foo', 25], ['xyz', 49]]
. How do convert sourcejson to targetjson in javascript.
Here's one way to do it:
var source = {"foo": 25, "xyz": 49}; var target = Object.keys(source).map(key => [key, source[key]]); console.log(target);
Another way to do this.
var sourceJsonStr= {"foo":25,"xyz":49}; var targetStrJson = []; for(var key in sourceJsonStr){ targetStrJson.push([key, sourceJsonStr[key]]); } console.log(targetStrJson);
Using .map
in es5
var sourceJsonStr = { "foo": 25, "xyz": 49 }; var targetStrJson = Object.keys(sourceJsonStr).map(function(key){ return [key, sourceJsonStr[key]]; }); console.log(targetStrJson);
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.