-1

Trying to create a function to return the Name if the price is the highest using javascript. Still learning.

  {
    Name: "Effective Yoga Habits",
    type: "book",
    price: 10.99
  },
  {
    Name: "Yoga kit",
    type: "product",
    price: 199.99
  },
  {
    Name: "Medative surroundings",
    type: "CD",
    price: 6.00
  }
]

If anyone can help I would appreciate it. Thank you in advance

Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42
GGcupie
  • 1,003
  • 1
  • 8
  • 11
  • Please show us your attempt so that we can help with your code, not just write code for you. – Bergi Jun 12 '20 at 15:03

1 Answers1

0

You may use Array.prototype.reduce() to traverse your array and have accumulator to store the record with greatest price value:

const src = [{Name:"Effective Yoga Habits",type:"book",price:10.99},{Name:"Yoga kit",type:"product",price:199.99},{Name:"Medative surroundings",type:"CD",price:6.00}],

    result = src
      .reduce((r, {Name,price}) => 
        price > r.price ? {Name,price} : r)
      .Name
    
console.log(result)
Yevhen Horbunkov
  • 14,965
  • 3
  • 20
  • 42