简体   繁体   中英

How to convert array to json object array?

I have the array in the following pattern:

[["abc","def"],["dss","ddd"]]

I need to convert it into an array of json objects:

[{"wf":"abc","sb":"def"},{"wf":"dss","sb":"ddd"}]

How would I do this?

Try this

var data= [["abc","def"],["dss","ddd"]];
var json = data.map(function (value, key) {
    return {
        "wf": value[0],
        "sb": value[1]
    }
});
console.log(json);
console.log(JSON.stringify(json)); // need string

DEMO

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM