I would like to split an object into two parts according to property "amount" (empty string)
let myObj = {
"1": {
"resources": "hotel",
"amount": "",
"currency": ""
},
"2": {
"resources": null,
"amount": "300.00",
"currency": "CZK"
},
"3": {
"resources": null,
"amount": "500.00",
"currency": "USD"
},
}
to this
obj1 = {
"1": {
"resources": "hotel",
"amount": "",
"currency": ""
}}
obj2 = {
"1": {
"resources": null,
"amount": "300.00",
"currency": "CZK"
},
"2": {
"resources": null,
"amount": "500.00",
"currency": "USD"
}}
I'm close to solving it but after numerous attempts (push, assign, map) it still does not work. Thx.
You can acheive your goal like this:
let myObj = {
"1": {
"resources": "hotel",
"amount": "",
"currency": ""
},
"2": {
"resources": null,
"amount": "300.00",
"currency": "CZK"
},
"3": {
"resources": null,
"amount": "500.00",
"currency": "USD"
},
}
const withAmount = {},
withoutAmount = {};
Object.keys(myObj).forEach(key => {
const item = myObj[key];
if (item.amount) {
withAmount[key] = item;
} else {
withoutAmount[key] = item
}
})
console.log('withAmount:',withAmount)
console.log('withoutAmount:',withoutAmount)