Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
filtering an array: get max price and unique objects?

store = [{
    "item": "shirt",
    "price": 20
  },
  {
    "item": "shirt",
    "price": 50
  },
  {
    "item": "pants",
    "price": 10
  },
  {
    "item": "pants",
    "price": 20
  }

]
//im filtering the array to get objects without duplication here
console.log(store.filter((v, i, a) => a.findIndex(v2 => ['item'].every(k => v2[k] === v[k])) === i))

and i would like to get the max price as well in the same filter so how would i get this output after excuting it ?

expected output:

[{
    "item": "shirt",
    "price": 50
 },
 {
    "item": "pants",
    "price": 20
}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could sort() them by price before filtering.

store = [{
    "item": "shirt",
    "price": 20
  },
  {
    "item": "shirt",
    "price": 50
  },
  {
    "item": "pants",
    "price": 10
  },
  {
    "item": "pants",
    "price": 20
  }
]

const result = store.sort((a, b) => b.price - a.price).filter((v, i, a) => a.findIndex(v2 => ['item'].every(k => v2[k] === v[k])) === i);


console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

I like @axtck's answer. .... Having filtered the objects, now use .map() to find from the original data the max price by sorting:

.map(
    ({item,price}) => 
    ({item,price:store.filter(p => p.item === item).sort((a,b) => b.price - a.price)[0].price})
)

store = [{
    "item": "shirt",
    "price": 20
  },
  {
    "item": "shirt",
    "price": 50
  },
  {
    "item": "pants",
    "price": 10
  },
  {
    "item": "pants",
    "price": 20
  }

]
//im filtering the array to get objects without duplication here
console.log(
    store.filter((v, i, a) => a.findIndex(v2 => ['item'].every(k => v2[k] === v[k])) === i)
    .map(
        ({item,price}) => 
        ({item,price:store.filter(p => p.item === item).sort((a,b) => b.price - a.price)[0].price})
    )
)

about 4 years ago · Juan Pablo Isaza Report

0

You could use Array.reduce() to get the highest price for each item.

We'd enumerate through each value, creating a map object, and if no entry exists for that item or the existing entry has a lower price, we'd update:

const store = [{ "item": "shirt", "price": 20 }, { "item": "shirt", "price": 50 }, { "item": "pants", "price": 10 }, { "item": "pants", "price": 20 } ]

const result = Object.values(store.reduce((acc, { item, price }) => {
    // Either item does not exist or has a lower price... 
    if (!acc[item] || acc[item].price < price) {
       acc[item] = { item, price };
    }
    return acc;
}, {}))

console.log(result)
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!