Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda