Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
Cómo encontrar el número más bajo de un objeto

Tengo un objeto como este:

 "data": [ { "type": "products", "id": "2021-01-04.1.1", "attributes": { "name": "product 1", "descriptions": "some description", "image": null, "date": "2021-01-04", "valid_from": null, "valig_untill": null, "xparc_paid_product_id": 1, "xparc_unpaid_product_id": 1, "xparc_access_id": 1, "stock": null, "variations": [ { "id": 1, "product_template_id": "1", "name": "child ticket", "description": "Only for children!", "price_excl_vat": 1, "created_at": "2021-09-15T13:16:00.000000Z", "updated_at": "2021-09-15T13:16:00.000000Z" }, { "id": 2, "product_template_id": "1", "name": "Adults", "description": "Not for children!", "price_excl_vat": 2, "created_at": "2021-09-15T13:16:10.000000Z", "updated_at": "2021-09-15T13:16:10.000000Z" } ] }, "links": { "self": "..." } }, ... ]

Entonces, como ve, los datos tienen un objeto dentro. Lo que estoy tratando de hacer es encontrar el precio más barato (price_excl_vat) dentro de las variaciones . Pero no sé cómo debo volver. He comenzado a crear esta función:

 priceCalculationCheapest(obj){ let arr = Object.values(obj); let min = Math.min(...arr); return min; },

Pero no puedo ir más lejos. ¿Podría por favor ayudarme con esto? Gracias

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Te refieres a esto

 const obj = { data: { "variations": [{ "id": 1, "product_template_id": "1", "name": "child ticket", "description": "Only for children!", "price_excl_vat": 1, "created_at": "2021-09-15T13:16:00.000000Z", "updated_at": "2021-09-15T13:16:00.000000Z" }, { "id": 2, "product_template_id": "1", "name": "Adults", "description": "Not for children!", "price_excl_vat": 2, "created_at": "2021-09-15T13:16:10.000000Z", "updated_at": "2021-09-15T13:16:10.000000Z" } ] } } const price = Math.min(...obj.data.variations .map(({price_excl_vat}) => price_excl_vat )) console.log(price)

about 4 years ago · Juan Pablo Isaza Denunciar

0

La mejor y más fácil manera de sort array . Si tiene menos registros. que será O(nlogn) . De lo contrario, puede usar el bucle for lineal para encontrar registros mínimos o máximos.

Muestra:

 const obj = { data: { variations: [ { id: 1, product_template_id: "1", name: "child ticket", description: "Only for children!", price_excl_vat: 1, created_at: "2021-09-15T13:16:00.000000Z", updated_at: "2021-09-15T13:16:00.000000Z", }, { id: 2, product_template_id: "1", name: "Adults", description: "Not for children!", price_excl_vat: 2, created_at: "2021-09-15T13:16:10.000000Z", updated_at: "2021-09-15T13:16:10.000000Z", }, ], }, }; const sortBy = (key, data = []) => { return data.sort((v1, v2) => { return v1[key] - v2[key]; // for reverse, max to min v2[key]-v1[key] }); }; const priceSorted = sortBy("product_template_id", obj.data.variations); console.log(priceSorted[0]); // min console.log(priceSorted[obj.data.variations.length - 1]); // max

about 4 years ago · Juan Pablo Isaza Denunciar

0

intente debajo del código.

 var obj = { "data": [{ "type": "products", "id": "2021-01-04.1.1", "attributes": { "name": "product 1", "descriptions": "some description", "image": null, "date": "2021-01-04", "valid_from": null, "valig_untill": null, "xparc_paid_product_id": 1, "xparc_unpaid_product_id": 1, "xparc_access_id": 1, "stock": null, "variations": [{ "id": 1, "product_template_id": "1", "name": "child ticket", "description": "Only for children!", "price_excl_vat": 1, "created_at": "2021-09-15T13:16:00.000000Z", "updated_at": "2021-09-15T13:16:00.000000Z" }, { "id": 2, "product_template_id": "1", "name": "Adults", "description": "Not for children!", "price_excl_vat": 2, "created_at": "2021-09-15T13:16:10.000000Z", "updated_at": "2021-09-15T13:16:10.000000Z" } ] }, "links": { "self": "..." } }] } function priceCalculationCheapest(arr) { let small = arr.reduce((first, second) => first.price_excl_vat < second.price_excl_vat ? first : second); return small; }; console.log(priceCalculationCheapest(obj.data[0].attributes.variations));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda