简体   繁体   中英

Converting JavaScript Array To JSON Format

i have an array like this coming back response from the server:

[
    "111",
    "1010",
    "111",
    "1010",
    "1010"
]

i want to convert it into a JavaScript JSON like this:

[
 {
    "branch":  "111"

 },
 {
    "branch":  "1010"
 },
 {
    "branch":  "111"
 },
 {
    "branch":  "1010"
 },
 {
    "branch":  "1010"
 }
]

You could map a short hand property.

 var array = ["111", "1010", "111", "1010", "1010"], result = array.map(branch => ({ branch })); console.log(result);

You can use map() to create a new array with the results of calling a provided function on every element in the calling array

 var arr = [ "111", "1010", "111", "1010", "1010" ] var res = arr.map(i => ({'branch': i})); console.log(res);

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