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

276
Vistas
How to find the lowest number an object

I have an object like this:

"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": "..."
      }
    }, ...
]

So as you see data has object inside. What I am trying to do is I want to find the cheapest price (price_excl_vat) inside variations. But I don't know how I should return. I have started to create this function:

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

But I cannot go further. Could you please help me about this? Thanks

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

0

You mean this

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

Best and easy way to sort array. If you have fewer records. which will be O(nlogn). Else you can use linear for loop to find min or max records.

Sample:

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

try below code.

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