Me gustaría dividir un objeto en dos partes según la propiedad "cantidad" (cadena vacía)
let myObj = { "1": { "resources": "hotel", "amount": "", "currency": "" }, "2": { "resources": null, "amount": "300.00", "currency": "CZK" }, "3": { "resources": null, "amount": "500.00", "currency": "USD" },}
a esto
obj1 = { "1": { "resources": "hotel", "amount": "", "currency": "" }} obj2 = { "1": { "resources": null, "amount": "300.00", "currency": "CZK" }, "2": { "resources": null, "amount": "500.00", "currency": "USD" }}Estoy cerca de resolverlo, pero después de numerosos intentos (empujar, asignar, mapear) todavía no funciona. Gracias.
Esta es la solución más simple y legible que he pensado.
const obj = { "1": { "resources": "hotel", "amount": "", "currency": "" }, "2": { "resources": null, "amount": "300.00", "currency": "CZK" }, "3": { "resources": null, "amount": "500.00", "currency": "USD" } } const withAmount = {}; const withoutAmount = {}; for(indexKey in obj) { const data = obj[indexKey]; if(data['amount'] != '') { withAmount[indexKey] = data; } else { withoutAmount[indexKey] = data; } } console.log({withAmount, withoutAmount});Puedes lograr tu objetivo así:
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)