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
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)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]); // maxintente 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));