I have an array which looks like this
var arr = [ [{a:1}],
[{b:1}],
[{c:1}],
[{d:1}],
[{e:1}] ]
what i want to do is to create a proper format out of it like this
[{a:1},{b:1},{c:1},{d:1},{e:1}]
tried many sources and not able to find the best solution
Can anyone help?
This should work for you (Not ideal but it works)
var arr = [ [{a:1}],
[{b:1}],
[{c:1}],
[{d:1}],
[{e:1}] ];
//this is not ideal but it works for u
for (var i = 0; i <arr.length - 1; i++) {
arr[i] = arr[i][0];
}
console.log(arr);
simply
const res = arr.flat()
or:
const arr = [ [{ a:1 }], [{b: 1}], [{c:1}], [{d:1}], [{e:1}] ] const res = arr.map(([o])=> ({...o})) // for a copy of objects console.log( JSON.stringify( res ))
or: (without makking a new array)
const arr = [ [{ a:1 }], [{b: 1}], [{c:1}], [{d:1}], [{e:1}] ] arr.forEach(([o],i,a)=>a[i]=o) console.log( JSON.stringify( arr ))
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.