最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find the most expensive item in an array - Stack Overflow

programmeradmin1浏览0评论

I have an array as such:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "puter",
    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.

I have an array as such:

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "puter",
    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.

Share Improve this question edited Aug 7, 2020 at 4:52 Always Helping 14.6k4 gold badges15 silver badges30 bronze badges asked Aug 7, 2020 at 4:36 Albert GonzalezAlbert Gonzalez 431 silver badge5 bronze badges 1
  • Does this answer your question? Finding the max value of an attribute in an array of objects and return the entire object or ES6 Find the maximum number of an array of objects – Karan Commented Aug 7, 2020 at 4:46
Add a ment  | 

4 Answers 4

Reset to default 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: "puter",
    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

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

let items = [
  {
    itemName: "Effective Programming Habits",
    type: "book",
    price: 13.99
  },
  {
    itemName: "Chromebook 2",
    type: "puter",
    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./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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.

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: "puter",
    price: 399.99
  },
 {
    itemName: "Programming 101",
    type: "book",
    price: 15.00
  } 
]
console.log([...items].sort((a,b) => a.price - b.price).pop().itemName)

发布评论

评论列表(0)

  1. 暂无评论