Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda