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

87
Vistas
Accumulated values into new keyed object in Javascript

My data:

const data = [{
    "id": 1,
    "location": 30,
    "year": 2019
  },
  {
    "id": 2,
    "location": 15,
    "year": 2000
  },
  {
    "id": 1,
    "location": 25,
    "year": 2019
  }
];

My desired outcome:

accumluatedData = [{
    "id": [1],
    "year": 2019,
    "frequency": 2
  },
  {
    "id": [2],
    "year": 2000,
    "frequency": 1
  }
];

What I have so far (with the kind help of other stackoverflowers), is getting into an array (see code below).

However, I would like to get it into an object (as shown above).

Any suggestions? Am I on the right track? Or should I use a different route?

const data = [{
    "id": 1,
    "location": 30,
    "year": 2019
  },
  {
    "id": 2,
    "location": 15,
    "year": 2000
  },
  {
    "id": 1,
    "location": 25,
    "year": 2019
  }
];


var results = [];

for (var i = 0; i < data.length; ++i) {
  if (results[data[i].year]) {
    results[data[i].year]++;
  } else {
    results[data[i].year] = 1;
  }
};

console.log("results", results);

about 4 years ago · Juan Pablo Isaza
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