magic bean
magic bean

Reputation: 797

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

Upvotes: 1

Views: 105

Answers (4)

Rahul Kumar
Rahul Kumar

Reputation: 3157

Iterate through each object in array and compare the price to find minimum.

const product = {
  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 getMinPrice = (list) => {
  let minPrice = list[0].price_excl_vat;
  list.forEach(obj => {
    if (obj.price_excl_vat < minPrice) {
      minPrice = obj.price_excl_vat;
    }
  });
  return minPrice;
}

console.log(getMinPrice(product.data.variations));

Upvotes: 1

xdeepakv
xdeepakv

Reputation: 8135

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

Upvotes: 2

Ganesan C
Ganesan C

Reputation: 277

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));

Upvotes: 2

mplungjan
mplungjan

Reputation: 178109

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)

Upvotes: 4

Related Questions