[
{
sessionInformation: [{id: 1, name: "Session first"}],
packageInformation: [{user: "Pol", id: 2}]
},
{
sessionInformation: [{id: 1, name: "Session second"}
},
{
sessionInformation: [{id: 1, name: "Session second"}]
}
]
{
packageInformation: [{user: "Pol", id: 2}]
}
[
{
sessionInformation: [{id: 1, name: "Session first"}],
packageInformation: [{user: "Pol", id: 2}]
},
{
sessionInformation: [{id: 1, name: "Session second"}
},
{
packageInformation: [{user: "Mike", id: 3}]
sessionInformation: [{id: 1, name: "Session second"}]
}
]
Result:
{
packageInformation: [{user: "Pol", id: 2}, {user: "Mike", id: 3}]
}
const data = [
{
sessionInformation: [{id: 1, name: "Session first"}],
packageInformation: [{user: "Pol", id: 2}]
},
{
sessionInformation: [{id: 1, name: "Session second"}]
},
{
packageInformation: [{user: "Mike", id: 3}],
sessionInformation: [{id: 1, name: "Session second"}]
}
]
const filtered = [].concat(...data.map(o => {
if (Array.isArray(o.packageInformation)) {
return o.packageInformation
}
return []
}))
console.log({
packageInformation: [].concat(...filtered)
})
function myFunc(carry, currentElem) {
if (currentElem.packageInformation && currentElem.packageInformation[0]) {
carry.packageInformation.push(currentElem.packageInformation[0])
}
return carry
}
var processed1 = data1.reduce(myFunc, {packageInformation: []})
var processed2 = data2.reduce(myFunc, {packageInformation: []})