I try to convert current array to expected array using some code , but it was not working. I tried react/ javacript
Currernt Array
[
{"name" : "john" ,"values" :["first","second","third","four"]},
{"name" : "sachin" ,"values" :["five","six","seven","eight"]},
]
Expected Array
[
{"name" : "john","a" : "first", "b" :"second", "c":"third", "d" : "four"},
{"name" : "sachin","a" : "five", "b" :"six", "c":"seven", "d" : "eight"}
]
const newArrayOfObj = this.state.data.map(({
values[0]: a,
values[1]: b,
values[2]: c,
values[3]: d,
....
}) => ({
a, b, c, d,
....
}));
5
Answers
I wrote a quick one here. Check if it could meet your need
You can try with this snippet
Here is a proposal
Use two nested reducers. The first passes over the outer array of object, the second passes over the values.
I ran it in TDD style:
An one-liner: