Negan
Negan

Reputation: 97

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);

Upvotes: 2

Views: 101

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386568

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; }

Upvotes: 1

Ramesh Reddy
Ramesh Reddy

Reputation: 10662

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));

Upvotes: 1

Related Questions