Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Dividir objeto en dos y volver a numerar claves

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.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

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});

about 4 years ago · Santiago Gelvez Report

0

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)

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!