const sizes = [1000, 750, 500]
const sum = 3000
function permutation(len, acc = []){
let res = []
sizes.forEach(s => {
let temp = null
if (s === len) {
res.push(acc.concat([s]))
} else if (s < len) {
temp = permutation(len-s, acc.concat([s]))
}
if (temp) {
res = res.concat(temp)
}
})
if (res.length) {
return res
}
}
const result = permutation(sum)
console.log(result)
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)