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

144
Views
Filtro de objetos JS basado en el recuento de valores únicos

considere el siguiente objeto JS de muestra

 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",}, ]

Quiero filtrar el objeto JS según el recuento único de valores y el orden descendente como el siguiente

según el valor cuenta AA - 5, AB - 2 y AC - 3 , pero necesito la salida como AA, AC

En reaccionar o JS, ¿cómo se puede lograr esto?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Mira esto:

 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 Report

0

Puede iterar sobre cualquier clave en los datos de muestra y contar cada aparición de un valor en un diccionario. Luego, puede imprimir el diccionario para obtener una lista de la cantidad de ocurrencias o solo la cantidad de ocurrencias de un valor.

 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'];

Salida: { CA: 3, AB: 2, AA: 5 } 5

about 4 years ago · Juan Pablo Isaza Report

0

El resultado deseado se puede encontrar siguiendo los siguientes pasos:

  1. Contar los valores.
  2. Ordene los pares valor/recuento por su recuento en orden descendente.
  3. Elimina el conteo, dejando solo el valor.

 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 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!