-1

I have an array as such:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "computer",
    price: 399.99
  },
 {
    itemName: "Programming 101",
    type: "book",
    price: 15.00
  } 
]

I need to create a function that loops through the array and finds the most expensive item and returns the itemName. I am brand new to JS and don't know really the best way to tackle this.

Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • Does this answer your question? [Finding the max value of an attribute in an array of objects and return the entire object](https://stackoverflow.com/questions/26895208/finding-the-max-value-of-an-attribute-in-an-array-of-objects-and-return-the-enti) or [ES6 Find the maximum number of an array of objects](https://stackoverflow.com/questions/48786855/es6-find-the-maximum-number-of-an-array-of-objects) – Karan Aug 07 '20 at 04:46

4 Answers4

4

You can reduce method. Check the prices and use . to access the max items name.

From MDN: The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

? is called ternary operator (its a short form of if and else)

More Info on how reduce works here

Live Demo:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "computer",
    price: 399.99
  },
 {
    itemName: "Programming 101",
    type: "book",
    price: 15.00
  } 
]

let maxItem = items.reduce((max, min) => max.price > min.price ? max : min);

console.log(maxItem.itemName) //Chromebook 2
console.log(maxItem) //Full object
Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • I have never used reduce, could you please explain what "? max : min" I understand the rest – Albert Gonzalez Aug 07 '20 at 04:43
  • @AlbertGonzalez min and max is just arg name. you can name it whatever you like. They way it works is its checks loops through the array and checks two object at a time show only where price is higher then the other using the `ternary operator`. Its like checking `if` and `else` to see which is higher in price and its shown only that object from the whole data. I will add most info in the answer. – Always Helping Aug 07 '20 at 04:46
  • extremely helpful, thank you very much! You have answered both my questions thus far superbly! – Albert Gonzalez Aug 07 '20 at 07:00
  • @AlbertGonzalez Happy to help always. No worries :) – Always Helping Aug 07 '20 at 07:01
2

You can use the Lodash library, it is very easy and fast to integrate. Lodash "maxBy" can find max value from array. https://lodash.com/docs/4.17.15#maxBy

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "computer",
    price: 399.99
  },
 {
    itemName: "Programming 101",
    type: "book",
    price: 15.00
  } 
]

console.log(_.maxBy(items, function(o) {
      return o.price;
 }));


  
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Arjun
  • 1,294
  • 13
  • 24
1

You can try this.

function getMostExp(items) {
  let mostExp = 0;
  let name;

  items.forEach(item => {
   if(item.price > mostExp) {
     mostExp = item.price;
     name = item.itemName;
   }
  });

  return name;
}

UPDATE:

Updated answer to return itemName instead of price.

codemax
  • 1,322
  • 10
  • 19
0

Sort the array by price, then pop off the last item and get the itemName:

items.sort((a,b) => a.price - b.price).pop().itemName

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "computer",
    price: 399.99
  },
 {
    itemName: "Programming 101",
    type: "book",
    price: 15.00
  } 
]
console.log([...items].sort((a,b) => a.price - b.price).pop().itemName)
dave
  • 62,300
  • 5
  • 72
  • 93