0

If I have this array:

const purchases = [
    {item: 'shoes', cost: 9, buyer: 'sheryl'},
    {item: 'candy', cost: 2, buyer: 'steve'},
    {item: 'candy', cost: 2, buyer: 'joe'},
    {item: 'apple', cost: 1, buyer: 'mary'},
    {item: 'candy', cost: 2, buyer: 'jeff'},
    {item: 'shoes', cost: 9, buyer: 'john'}];

I want to find the Item that sells the most, and how many were bought returned in an object like this:

{item: 'candy', sales: 3}

if there are ties, just return one of them.

This is the most simple way I can put it. I have found many questions similar to this but none that had a solution to this exactly. Can someone please help?

supafiya
  • 137
  • 2
  • 9
  • This should answer your question. https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array – Benjamin James Kippax Mar 05 '20 at 02:55
  • 1
    Does this answer your question? [Get the element with the highest occurrence in an array](https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array) – Benjamin James Kippax Mar 05 '20 at 02:55
  • Please refere this link. https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects Hope this helps – Don Mar 05 '20 at 02:57
  • none of these are helping me, I saw a few of those before but they are just array items I don't know how to get it working with an array of objects like I have – supafiya Mar 05 '20 at 05:39

0 Answers0