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)
})
const data = {
param: 'value',
key: {
nested: {
double_nested: 'value'
}
},
nested: {
array: ['key 1', 'key 2']
}
}
function getObjectKeys(obj, prefix = '') {
const keys = Object.keys(obj).reduce((res, el) => {
if (Array.isArray(obj[el])) {
return [...res, ...obj[el].map((item, i) => `${prefix}${el}[${i}]`)]
} else if (obj[el] !== null && typeof obj[el] === 'object') {
return [...res, ...getObjectKeys(obj[el], prefix + el + '.')]
} else {
return [...res, prefix + el]
}
}, [])
keys.sort()
return keys
}
const array = getObjectKeys(data)
console.log(array)