i have an array like this coming back response from the server:
[
[
"111",
1
],
[
"1010",
4
],
[
"111",
5
],
[
"1010",
6
],
[
"1010",
7
]
]
i want to convert it into a JavaScript JSON like this:
[
{
"branch": "111",
"id":1
},
{
"branch": "1010",
"id":4
},
{
"branch": "111",
"id":5
},
{
"branch": "1010",
"id":6
},
{
"branch": "1010",
"id":7
}
]
If any one can help it will be much appreciable. Bcs i am new to javascript
You can do this with Array#map and destructuring .
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
const data = [["111",1],["1010",4],["111",5],["1010",6],["1010",7]]; const res = data.map(([branch,id])=>({branch,id})); res.sort((a,b)=>a.id-b.id); 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.