-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.

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

I have never used reduce, could you please explain what "? max : min" I understand the rest
@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.
extremely helpful, thank you very much! You have answered both my questions thus far superbly!
@AlbertGonzalez Happy to help always. No worries :)
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>

Comments

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.

1 Comment

OP is asking for the Item Name, not the Item Price.
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)

4 Comments

Just a minor thing, there is no need to use a spreader operator here.
@KarlL if you don't use a spreader you'll remove the item from the original array, which may not be what they want to do.
good point. I agree. just in case the OP needs the original data
@dave why use pop. just sort the arr in rev order and use [0]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.