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

129
Views
Copiar objeto que tiene un valor específico

Tengo un escenario en el que necesito copiar las ofertas (sin precio) si el precio es mayor que 0 del siguiente json:

 const data = [ { "id": "offer1", "Identifier": { "info": "some info for offer1", "value": "some string for offer1" }, "Price": { "Total": 94.30 } }, { "id": "offer2", "Identifier": { "info": "some info for offer2", "value": "some string for offer2" }, "Price": { "Total": 0.0 } }, { "id": "offer3", "Identifier": { "info": "some info for offer3", "value": "some string for offer3" }, "Price": { "Total": 48.50 } } ];

Necesito obtener todas las ofertas donde el precio es > 0 y colocarlas en otra matriz con un formato específico, el resultado esperado debería verse así:

 const result = [ { "id":"offer1", "reference":"offer1", "Identifier":{ "info":"some info for offer1", "value":"some string for offer1" } }, { "id":"offer3", "reference":"offer3", "Identifier":{ "info":"some info for offer3", "value":"some string for offer3" } } ]

El primer paso que hice fue filtrar la matriz de datos inicial para excluir ofertas con un Total = 0.

 const filteredOffers = data.filter(offer => offer.Price.Total > 0);

¿Qué debo hacer a continuación para obtener el resultado esperado? Gracias.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Podrías mapear nuevos objetos.

 const data = [{ id: "offer1", Identifier: { info: "some info for offer1", value: "some string for offer1" }, Price: { Total: 94.3 } }, { id: "offer2", Identifier: { info: "some info for offer2", value: "some string for offer2" }, Price: { Total: 0 } }, { id: "offer3", Identifier: { info: "some info for offer3", value: "some string for offer3" }, Price: { Total: 48.5 } }], result = data .filter(({ Price: { Total } }) => Total > 0) .map(({ Price, ...o }) => ({ ...o, reference: o.id })); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el método de reducción para mapear los artículos y solo devolver aquellos con un precio que no sea igual a 0.

 function formatOffers(data) { return data.reduce((acc, item) => { const { id, Identifier, Price } = item; if (Price.Total === 0) { return acc; } else { return [...acc, { id, reference: id, Identifier, Price }]; } }, []); }
about 4 years ago · Juan Pablo Isaza Report

0

El siguiente paso puede ser:-

 const offersWithoutPrice = filteredOffers.map(offer => { delete offer["Price"]; return offer; });
about 4 years ago · Juan Pablo Isaza 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!