• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

25
Views
JS How to compare an array of objects and get the lowest value together with object data

I have a product in multiple variations

{
  product: {
    name: 'TShirt',
    brand: 'Some brand",
    variations: [
       {
         color: 'red',
         sku: '1234',
         price: 129,
         id: 1
       },
       {
         color: 'black',
         sku: '123442',
         price: 99,
         id: 2,
       },
       {
         color: 'yellow',
         sku: '1234423',
         price: 79,
         id: 3
       }
    ]
  }
}

Now I want to get and display the cheapest variation of the product. I've managed to get the cheapest variants price:

const min = Math.min(...variations.map((item) => item.price))

but how can I get the rest of the object? In this example the 'yellow' variant?

about 2 months ago ·

Juan Pablo Isaza

Answer question
Find remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.