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

210
Vistas
Javascript .reduce() tips? Is there a way to rename 'undefined' group?

I'm trying to understand the .reduce() function and the best way to go about the following.

I've got the following code:

const products = [
  { name: 'apples', category: 'fruits' },
  { name: 'oranges', category: 'fruits' },
  { name: 'potatoes', category: 'vegetables' }
];

const groupByCategory = products.reduce((group, product) => {
  const { category } = product;
  group[category] = group[category] ?? [];
  group[category].push(product);
  return group;
}, {});

I want to add a product with no 'category' property in it, and I want it pushed into a specific key rather than getting grouped in "undefined", so I edited it to:

const products = [
  { name: "apples", category: "fruits" },
  { name: "oranges", category: "fruits" },
  { name: "potatoes", category: "vegetables" },
  { name: "guava"}
];

const groupByCategory = products.reduce((group, product) => {

  const { category } = product ;

  // check if 'category' exists, if it doesn't store it as an empty array to push to
  group[category] = group[category] ?? []; 
  
  // if category is undefined, push it into 'nocategory'. Otherwise push into relevant.
  if(!category){
     group['nocategory'].push(product);
  } else {
    group[category].push(product);
  };
  return group;
}, {'nocategory':[]});

console.log(JSON.stringify(groupByCategory, null, 2));

For the most part it works (there's still an 'undefined' group, but at least the object gets pushed into the right group).

I'm sure there's a better solution/proper way to do this. Any pointers would be appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are creating undefined here

group[category] = group[category] ?? []; // category can be undefined

Move creation into if-else statement

const products = [
  { name: "apples", category: "fruits" },
  { name: "oranges", category: "fruits" },
  { name: "potatoes", category: "vegetables" },
  { name: "guava"}
];

const groupByCategory = products.reduce((group, product) => {

  const { category } = product ;

  // check if 'category' exists, if it doesn't store it as an empty array to push to
  // removed
  
  // if category is undefined, push it into 'nocategory'. Otherwise push into relevant.
  if(!category){
     group['nocategory'].push(product);
  } else {
    group[category] = group[category] ?? [] // HERE
    group[category].push(product);
  };
  return group;
}, {'nocategory':[]});

console.log(JSON.stringify(groupByCategory, null, 2));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Instead of a whole new conditional block you could just set a default in the destructuring and then group as usual.

const { category = 'nocategory' } = product;

const products = [
  { name: "apples", category: "fruits" },
  { name: "oranges", category: "fruits" },
  { name: "potatoes", category: "vegetables" },
  { name: "guava"}
];

const groupByCategory = products.reduce((group, product) => {

  const { category = 'nocategory' } = product;
  group[category] ??= []; 
  group[category].push(product);

  return group;
}, {});

console.log(JSON.stringify(groupByCategory, null, 2));

Note: you can also make use of logical nullish assignment (??=)

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