There is an target array inside of an array object:
[
{
_id: 'main1',
target: [
{ _id: '1', foo: [bar] },
{ _id: '2', foo: [bar] }
]
},
{
_id: 'main2',
target: [
{ _id: '3', foo: [bar] },
{ _id: '4', foo: [bar] }
]
}
]
I need to get all target objects as one array:
Needed result
targets: [
{ _id: '1', foo: [bar] },
{ _id: '2', foo: [bar] }
{ _id: '3', foo: [bar] },
{ _id: '4', foo: [bar] }
]
I tried to use a map()
array.map(item => item.target)
But this results in a nested array like: [ [ { _id: '1', foo: [bar] } ] ]
var array = [
{
_id: 'main1',
target: [
{ _id: '1', foo: ["bar"] },
{ _id: '2', foo: ["bar"] }
]
},
{
_id: 'main2',
target: [
{ _id: '3', foo: ["bar"] },
{ _id: '4', foo: ["bar"] }
]
}
]
console.log(
array.map(item => item.target)
)