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

157
Visualizações
JS Object filter based on count of unique values

consider the following sample JS Object

const samplejson = [
    {id:'1',value: "AC",},
    {id:'2',value: "AB",},
    {id:'3',value: "AC",},
    {id:'4',value: "AA",},
    {id:'5',value: "AA",},
    {id:'6',value: "AA",},
    {id:'7',value: "AB",},
    {id:'8',value: "AC",},
    {id:'9',value: "AA",},
    {id:'10',value: "AA",},
]

I want to filter the JS object based on the values unique count and descending order like the following

based on the value counts AA - 5, AB - 2 and AC - 3, but I need the output as AA,AC

In react or JS how can achieve this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Check this out:

const data = [{id:'1',value: "AC",},{id:'2',value: "AB",},{id:'3',value: "AC",},    {id:'4',value: "AA",},{id:'5',value: "AA",},{id:'6',value: "AA",},{id:'7',value: "AB",},    {id:'8',value: "AC",},{id:'9',value: "AA",},{id:'10',value: "AA",},];

const result = Object.entries(
  data.reduce((acc, { value }) => ({ ...acc, [value]: (acc[value] || 0) + 1 }), {})
).sort((a1, a2) => a2[1] - a1[1])
.map(([key]) => key)
.join(',');

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could iterate over any key in the sample data and count each occurrence of a value in a dictionary. Then you can both print the dictionary for a list of amount of occurrences or just the amount of occurrences of one value.

const data = [
    {id:'1',value: "AC",},
    {id:'2',value: "AB",},
    {id:'3',value: "AC",},
    {id:'4',value: "AA",},
    {id:'5',value: "AA",},
    {id:'6',value: "AA",},
    {id:'7',value: "AB",},
    {id:'8',value: "AC",},
    {id:'9',value: "AA",},
    {id:'10',value: "AA",},
]


// make a dict to store and count occurrences
dict = {};

//iterate over every key in your data and keep count in dict
//(initializing it to 1 if it not exists yet)
for(let i = 0; i < data.length; i++){
  value = data[i].value;
  if(value in dict){
    dict[value]++;
  }else{
    dict[value] = 1;
  }
}

// here the amount of occurrences are know
console.log(dict);
amountOfAA = dict['AA'];

Output: { AC: 3, AB: 2, AA: 5 } 5

about 4 years ago · Juan Pablo Isaza Relatório

0

Your desired result can be found using the following steps:

  1. Tally the values.
  2. Sort the value/count pairs by their count in descending order.
  3. Remove the count, leaving only the value.

const items = [
  { id:  '1', value: "AC" },
  { id:  '2', value: "AB" },
  { id:  '3', value: "AC" },
  { id:  '4', value: "AA" },
  { id:  '5', value: "AA" },
  { id:  '6', value: "AA" },
  { id:  '7', value: "AB" },
  { id:  '8', value: "AC" },
  { id:  '9', value: "AA" },
  { id: '10', value: "AA" },
];

const tally = new Map();
for (const { value } of items) {
  if (!tally.has(value)) tally.set(value, 0);
  tally.set(value, tally.get(value) + 1);
}

// displaying as object because Map instances show empty in the snippet log
console.log(Object.fromEntries(tally));

const results = Array.from(tally)
  .sort(([,countA], [,countB]) => countB - countA)
  .map(([value]) => value);

console.log(results);

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