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

166
Vistas
JavaScript: group array of objects by property in an object

if I have a js array like below, is there a simple way to re-group the array values by age? I tried to reduce it to an array but it did not help. it's re-grouped with age or name's

let employees = [
 {
  firstName:"Zion",
  lastName: "Rosy",
  age: 25,
  joinedDate:"January 24, 2020",
 },

 {
  firstName: "Ana",
  lastName: "Rosy",
  age: 25,
  joinedDate: "January 15, 2019",
 },

 {
  firstName: "Zion",
  lastName:"Albert",
  age: 30,
  joinedDate:"February 15, 2011",
 },
];

To re-group by age like this:

let organizedByAge =
 {
  25: [
 {
  firstName:"Zion",
  lastName: "Rosy",
  age: 25,
  joinedDate:"January 24, 2020",
 },
 {
  firstName: "Ana",
  lastName: "Rosy",
  age: 25,
  joinedDate: "January 15, 2019",
 },
  ],

  30: [
   {
    firstName:"Zion",
    lastName:"Albert",
    age: 30,
    joinedDate:"February 15, 2011",
   },
  ],
 };
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

is there a simple way to re-group the array values by age?

When rewriting arrays and objects, the most straightforward and readable approach is often to use a simple for loop:

for (let i = 0; i < employees.length; i++) {
  
  const age = employees[i]['age'];
  
  if (organizedByAge.hasOwnProperty(age)) {
    organizedByAge[age].push(employees[i]);
  }
  
  else {
    organizedByAge[age] = [];
    organizedByAge[age][0] = employees[i];
  }
}

Working Example:

let employees = [
 {
  firstName:"Zion",
  lastName: "Rosy",
  age: 25,
  joinedDate:"January 24, 2020",
 },

 {
  firstName: "Ana",
  lastName: "Rosy",
  age: 25,
  joinedDate: "January 15, 2019",
 },

 {
  firstName: "Zion",
  lastName:"Albert",
  age: 30,
  joinedDate:"February 15, 2011",
 },
];

let organizedByAge = {};

for (let i = 0; i < employees.length; i++) {
  
  const age = employees[i]['age'];
  
  if (organizedByAge.hasOwnProperty(age)) {
    organizedByAge[age].push(employees[i]);
  }
  
  else {
    organizedByAge[age] = [];
    organizedByAge[age][0] = employees[i];
  }
}

console.log(organizedByAge);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using Array#reduce:

const employees = [
  { firstName:"Zion", lastName: "Rosy", age: 25, joinedDate:"January 24, 2020" },
  { firstName: "Ana", lastName: "Rosy", age: 25, joinedDate: "January 15, 2019" },
  { firstName: "Zion", lastName:"Albert", age: 30, joinedDate:"February 15, 2011" }
];

const organizedByAge = employees.reduce((map, e) => ({
  ...map,
  [e.age]: [...(map[e.age] ?? []), e]
}), {});

console.log(organizedByAge);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const groupByKey = (_data, _key) => {
  return _data.reduce((result, next) => {
    const key = next[_key];
    result[key] = result[key]?.length ? [...result[key], next] : [next];
    return result;
  }, {});
  }

 console.warn(groupByKey(employees, 'age'));
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