2

I am trying to convert an array of objects containing string values to their id value based off other array of objects. Here are the arrays.

const employees = [
  {
    name: 'bob',
    department: 'sales',
    location: 'west'
  },
  {
    name:'fred',
    department: 'sales',
    location: 'west'
  },
  {
    name:'josh',
    department: 'inventory',
    location: 'east'
  },
  {
    name: 'mike',
    department: 'quality assurance',
    location: 'north'
  }
];

const departments = [
 {
    dep: 'sales',
    id: 12
 },
 {
    dep:'quality assurance',
    id: 11
 },
 {
    dep:'inventory',
    id: 13
 }
];

const locations = [
  {
    region: 'west',
    id: 3
  },
  {
    region:'north',
    id: 1
  },
  {
    region:'east',
    id: 2
  },
  {
    region:'south',
    id: 4
  }
];

I would like the converted employees array to look like this:

[
 {name:"bob", department: 12, location: 3},
 {name:"fred", department: 12, location: 3},
 {name:"josh", department: 13, location: 2},
 {name:"mike", department: 11, location: 1}
]

I've tried:

employees.forEach((row) => {
  row.department = departments.filter(depart => row.department === depart.dep)
  .reduce((accumulator, id) => id)
  row.department = row.department.id; // would like to remove this.
});
employees.forEach((row) => {
  row.location = locations.filter(loc => row.location === loc.region)
  .reduce((accumulator, id) => id);
  row.location = row.location.id; // would like to remove this part.
});

I get the desired results from using the forEach I have, but I think there is a better way of using .filter() and .reduce(). I would like help removing the last line of the two forEach statements where I have to set row.department = row.department.id and row.location = row.location.id

3 Answers 3

5

One possible approach:

const dehydratedEmployees = employees.map(emp => {
  const depId = departments.find(dep => dep.dep === emp.department).id;
  const locId = locations.find(loc => loc.location === loc.region).id;
  return { name: emp.name, department: depId, location: locId };
});

In other words, you can use Array.prototype.find() instead of filter-reduce combo. As .reduce() won't stop at the first successful search, .find() is both more efficient and concise. Just don't forget to apply polyfill for IE and other non-supportive browsers.

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

Comments

2

One solution is to create Map for departments and locations to eliminated nested loop when mapping employees.
Map can be created from a nested array: new Map([[key, value], [key, value]]):

const employees = [
  { name: 'bob', department: 'sales', location: 'west' },
  { name:'fred', department: 'sales', location: 'west' },
  { name:'josh', department: 'inventory', location: 'east' },
  { name: 'mike', department: 'quality assurance', location: 'north'}
];

const departments = [
 { dep: 'sales', id: 12 },
 { dep:'quality assurance', id: 11 },
 { dep:'inventory', id: 13}
];

const locations = [
  { region: 'west', id: 3 },
  { region:'north', id: 1},
  { region:'east', id: 2 },
  { region:'south', id: 4}
];

const departmentMap = new Map(departments.map(i => [i.dep, i.id]));
const locationMap = new Map(locations.map(i => [i.region, i.id]));
const result = employees.map(e => ({
  name: e.name,
  department: departmentMap.get(e.department),
  location: locationMap.get(e.location)
}))
console.log(result);

1 Comment

Welcome. Good answer :)
1

Another possible approach. You can use Array.prototype.filter()(like below)

const employees=[{name:'bob',department:'sales',location:'west'},{name:'fred',department:'sales',location:'west'},{name:'josh',department:'inventory',location:'east'},{name:'mike',department:'quality assurance',location:'north'}];const departments=[{dep:'sales',id:12},{dep:'quality assurance',id:11},{dep:'inventory',id:13}];const locations=[{region:'west',id:3},{region:'north',id:1},{region:'east',id:2},{region:'south',id:4}]

var newArray=employees.map((x)=>{
    return { name: x.name,
    department: departments.filter(y=>y.dep === x.department)[0].id,
    location: locations.filter(y=>y.region===x.location)[0].id};
});
console.log(newArray);

Comments

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.