Printing the second lowest in an Object array

function secondCheapest(bookObj){
  const price = bookObj.map(bookObj => {
    return bookObj.price
    })
 const name = bookObj.map(bookObj => {
   return bookObj.title
 })
 
  let lowest = Math.min(...price)
  let second = price.filter(value => value != lowest)
  let n = Math.min(...second)
return n
}
console.log(secondCheapest([
    {title:'Fox In Socks', price:32.20},
    {title:'The Cat In The Hat', price:16.20},
    {title:'Green Eggs and Ham', price:28.50},
    {title:'Thinking Fast and Slow', price:58.80},
    {title:'War and Peace', price: 5.20}
]));

I
I’m stuck here please help… I’m trying to return the second lowest price of the book here, but my code is printing the second price only. How can I make the code to print both the title and the price such as… title:‘The Cat In The Hat’, price:16.20

The variable n has the second lowest price. It is possible to filter the input array bookObj based upon the condition on the price property value matching the value of n.

I got the correct output, just that the price doesn’t include the title

Do you need to return this?

{ title: 'The Cat In The Hat', price: 16.2 }

Just filter the array as said above. Your n === 16.2, so it’s correct, but it’s just the number.

For your solution, you dont need this:

Yes. I want to print the title and the book price. My code is only printing the price

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.