I am struggling with an issue that is bugging me a lot. I am not good with JSON data manipulation.
So the issue is I have an Array of multiple Object which contain some data, Now inside these objects, I have another array of objects which I want
the JSON looks something like this-
const data = [{
a: 2,
b: 3,
c: 4,
d: [{
e: 5,
f: 4,
g: 6,
}, {
h: 5,
i: 4,
j: 6,
}]
}, {
a: 11,
b: 31,
c: 42,
d: [{
e: 54,
f: 46,
g: 62,
}, {
h: 55,
i: 42,
j: 64,
}]
}]
Now What I want is an Array which holds the following data
const d = [{
e: 5,
f: 4,
g: 6,
}, {
h: 5,
i: 4,
j: 6,
}, {
e: 54,
f: 46,
g: 62,
}, {
h: 55,
i: 42,
j: 64,
}]
I tried mapping over the data but I always end up with an array that look
const data = [
[{
e: 5,
f: 4,
g: 6,
}, {
h: 5,
i: 4,
j: 6,
}],
[{
e: 54,
f: 46,
g: 62,
}, {
h: 55,
i: 42,
j: 64,
}]
]
Not sure what am i doing wrong. need some help
CodePudding user response:
You can loop over the data
and then loop over the data.d
again to get all of the required data. For each of the data, you push it into another array that you will later use.
const data = [{a:2,b:3,c:4,d:[{e:5,f:4,g:6,},{h:5,i:4,j:6,}]},{a:11,b:31,c:42,d:[{e:54,f:46,g:62,},{h:55,i:42,j:64,}]}]
let result = [];
data.forEach(d => d.d.forEach(dd => result.push(dd)));
console.log(result);