0

I have this problem where the array of objects is intendedly put into this kind of structure:

const productArray = [
  { 'Product A': 5000 },
  { 'Product B': 2500 },
  { 'Product C': 3500 },
  { 'Product D': 2750 },
  { 'Product E': 1500 },
];

On what way could I determine the highest and lowest value of this kind of array? Thank you very much.

Shironekomaru
  • 361
  • 1
  • 6
  • 15
  • 1
    You're probably better off using a different structure. Either use a single object with different properties, or use an array with the values. Or alternatively an array of objects with both the product name and its value as separate properties. Having an array of objects where every object has a different property is making your own live needlessly hard. – Ivar Sep 13 '21 at 07:37
  • @ivar I wish it was the case. But the problem was intendedly put it in this structure. – Shironekomaru Sep 13 '21 at 07:45

1 Answers1

1

Sort the array, Lowest one will be at first and Largest one will be at last

const productArray = [
  { 'Product A': 5000 },
  { 'Product B': 2500 },
  { 'Product C': 3500 },
  { 'Product D': 2750 },
  { 'Product E': 1500 },
];
const sortedProduct = productArray.sort((a, b) => Object.values(a)[0] - Object.values(b)[0]);
console.log(sortedProduct);
console.log('Lowest value ', sortedProduct[0]);
console.log('Highest value ', sortedProduct[sortedProduct.length - 1]);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • 1
    [You can also use `Math.max` and `Math.min`](https://jsfiddle.net/3pm9gbsd/). – Andy Sep 13 '21 at 07:54
  • Thank you so much @andy. I'll try to understand this as this gave a better outcome for my other key:value pair. – Shironekomaru Sep 13 '21 at 08:05
  • 1
    No problem. [I've updated the fiddle](https://jsfiddle.net/ns1k49pL/) with some comments about how it works, and some links to the relevant documentation. @Shironekomaru – Andy Sep 13 '21 at 08:11
  • @Andy just a bit of something again. Is there a way to call the corresponding key of that result, as well? – Shironekomaru Sep 13 '21 at 08:15
  • How do you mean? [Do you want to return the object from the array that matches either the min or the max?](https://jsfiddle.net/n39hk7ga/)? – Andy Sep 13 '21 at 08:20
  • @Andy um... I mean now that the values are paired with index keys during the map. Is there still a way to call the old key that responds to the min and max result? like in this case: the "Product A" with 5000 is the max result and "Product E" is the min result instead of the index keys 0 and 4 – Shironekomaru Sep 13 '21 at 08:28
  • 1
    [Use `find` to get the object with that value, and return its first key.](https://jsfiddle.net/n39hk7ga/1/). – Andy Sep 13 '21 at 08:33
  • @Andy wow. Thank you very much and lots of hugs. You answered all of my problems. – Shironekomaru Sep 13 '21 at 08:43
  • 1
    Thanks! There's probably a clever one-liner in there somewhere but I hope the steps I outlined made it easier to understand. @Shironekomaru. – Andy Sep 13 '21 at 08:46