2

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);
1
  • For example, instead of map.set(obj.lastName, obj), you could write map.set(obj.lastName, { ...obj, fullName: obj.firstName + " " + obj.lastName }) and remove the map. Personally, I prefer your split approach, as it’s more readable, but I would chain method calls as much as possible. You could also consider using Iterator Helpers. Commented Dec 31, 2021 at 12:00

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Could you explain what exactly is happening in the second filter ".filter((s => ({ lastName }) => !s.has(lastName) && s.add(lastName))(new Set))" ? How is the set being created? I usually will see filter with one return arrow function and not two. I'm curious how the "s" variable becomes a "new Set()" to be used in the second arrow function.
please see edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.