Converting JavaScript Array To JSON Format

后端 未结 2 1305
滥情空心
滥情空心 2021-01-29 14:16

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

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

i want t

相关标签:
2条回答
  • 2021-01-29 14:53

    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);

    0 讨论(0)
  • 2021-01-29 14:59

    You could map a short hand property.

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

    0 讨论(0)
提交回复
热议问题