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

187
Vistas
Efficiently Combining Multiple Array Method Transformations in Javascript

I have a working program that satisfies the conditions below but I'm wondering if there is a more efficient solution. Currently, I apply 4 different Javascript array method transformations which results in returning a new array for each transformation for a total of 4 new arrays. Can these transformations be combined to only create 1 additional array instead of 4 new arrays? I could chain together the calls but I was thinking it might be possible to combine them all into reduce() method but I'm not sure how that would look or if there is some better solution.

The criteria that needs to be satisfied:

  1. Only include employees from the Google organization but allow this to be passed as an input parameter
  2. Last names should be unique (no duplicate last names)
  3. Employees should be sorted by ID (ascending)
  4. Each employee should have an added property called fullName that is a combination of first and last names, separated by a space
const GOOGLE_ORG = 'Google';

const employees = [
  {
    id: 3,
    firstName: 'John',
    lastName: 'Doe',
    organization: 'Google',
  },
  {
    id: 7,
    firstName: 'Jake',
    lastName: 'Smith',
    organization: 'Google',
  },
  {
    id: 1,
    firstName: 'Jane',
    lastName: 'Doe',
    organization: 'Google',
  },
  {
    id: 2,
    firstName: 'Vanessa',
    lastName: 'Smith',
    organization: 'Meta',
  },
  {
    id: 5,
    firstName: 'Sarah',
    lastName: 'Hernandez',
    organization: 'Meta',
  },
  {
    id: 8,
    firstName: 'Jessica',
    lastName: 'Morales',
    organization: 'Google',
  },
  {
    id: 4,
    firstName: 'Paul',
    lastName: 'Stark',
    organization: 'Google',
  },
  {
    id: 6,
    firstName: 'Peter',
    lastName: 'Brown',
    organization: 'Meta',
  },
];

const transformArray = (org) => {
  const filteredByOrg = employees.filter((employee) => employee.organization === org);
  const addedFullName = filteredByOrg.map((employee) => ({
    ...employee,
    fullName: employee.firstName + ' ' + employee.lastName,
  }));
  const uniqueByLastName = [...addedFullName.reduce((map, obj) => map.set(obj.lastName, obj), new Map()).values()];
  return uniqueByLastName.sort((a, b) => a.id - b.id);
};

transformArray(GOOGLE_ORG);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yes, you can use Array.prototype.reduce to minimize creating arrays and the number of iterations.

const GOOGLE_ORG = 'Google';

const employees = [
  {
    id: 3,
    firstName: 'John',
    lastName: 'Doe',
    organization: 'Google',
  },
  {
    id: 7,
    firstName: 'Jake',
    lastName: 'Smith',
    organization: 'Google',
  },
  {
    id: 1,
    firstName: 'Jane',
    lastName: 'Doe',
    organization: 'Google',
  },
  {
    id: 2,
    firstName: 'Vanessa',
    lastName: 'Smith',
    organization: 'Meta',
  },
  {
    id: 5,
    firstName: 'Sarah',
    lastName: 'Hernandez',
    organization: 'Meta',
  },
  {
    id: 8,
    firstName: 'Jessica',
    lastName: 'Morales',
    organization: 'Google',
  },
  {
    id: 4,
    firstName: 'Paul',
    lastName: 'Stark',
    organization: 'Google',
  },
  {
    id: 6,
    firstName: 'Peter',
    lastName: 'Brown',
    organization: 'Meta',
  },
];

const transformArray = (org) => {
  return (employees.reduce((lastNames => ((acc, curr) => {
    if(!lastNames.has(curr.lastName) && curr.organization === org) {
        lastNames.add(curr.lastName)
        acc.push({...curr, fullName: `${curr.firstName} ${curr.lastName}`})
    }
    return acc;
  }))(new Set()), []).sort((a, b) => a.id - b.id));
};

console.log(transformArray(GOOGLE_ORG));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could chain all operations.

Actually it returns a different reuslt by filtering unique lastnames and sorting after or by sorting first and then filtering.

An example of a closure:

Take this line for filtering with a Set:

.filter((s => ({ lastName }) => !s.has(lastName) && s.add(lastName))(new Set))

A closure takes variables into the scope of the calling function and returns another function. This function has access to the variable. Let's have a look with a different formatting:

.filter(
    (s => ({ lastName }) => !s.has(lastName) && s.add(lastName))
    (new Set)
)

Here, the function is called with new Set and this function is returned

          ({ lastName }) => !s.has(lastName) && s.add(lastName)

as callback for the filter method.

const
    GOOGLE_ORG = 'Google',
    employees = [{ id: 3, firstName: 'John', lastName: 'Doe', organization: 'Google' }, { id: 7, firstName: 'Jake', lastName: 'Smith', organization: 'Google' }, { id: 1, firstName: 'Jane',  lastName: 'Doe', organization: 'Google' }, { id: 2, firstName: 'Vanessa', lastName: 'Smith', organization: 'Meta' }, { id: 5, firstName: 'Sarah', lastName: 'Hernandez', organization: 'Meta' }, { id: 8, firstName: 'Jessica', lastName: 'Morales', organization: 'Google' }, { id: 4, firstName: 'Paul', lastName: 'Stark', organization: 'Google' }, { id: 6, firstName: 'Peter', lastName: 'Brown', organization: 'Meta' }],
    result = employees
        .filter(({ organization }) => organization === GOOGLE_ORG)
        .filter((s => ({ lastName }) => !s.has(lastName) && s.add(lastName))(new Set))
        .sort((a, b) => a.id - b.id)
        .map(o => ({ ...o, fullName: o.firstName + o.lastName }));
        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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