Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda