Skip to main content
added 1 character in body
Source Link
toolic
  • 15.8k
  • 6
  • 29
  • 217

Use a Map

Existing answers are using Object.groupBy to create a map of departments grouped by id.

This is inappropriate as there is only a one to one relationship between department id and departments. Object.groupBy creates groups using arrays to hold members of each group. One to many.

Using a more appropriate Map to get departments by id will simplify the code a little as there is no need to do the additional array indexing when locating a department.

Don`tDon't duplicate data

NoNor can I see why you duplicate the department_id as employee_department_id which is redundant information.

const employees = [{name: 'Alice', department_id: 12}, {name: 'Bob', department_id: 13}, {name: 'Chris', department_id: 13}, {name: 'Dan', department_id: 14}, {name: 'Eve', department_id: null}];
const departments = [{department_id: 12, name: 'Sales'}, {department_id: 13, name: 'Marketing'}, {department_id: 14, name: 'Engineering'}, {department_id: 15, name: 'Accounting'}, {department_id: 16, name: 'Operations'}];

console.log(innerJoinEmpDep(departments, employees));

function innerJoinEmpDep(deps, emps) {
    const depsById = new Map(deps.map(dep=> ([dep.department_id, dep])));
    const innerJoin = [];
    for (const emp of emps) {
        const dep = depsById.get(emp.department_id);
        if (dep) {
            innerJoin.push({
                employ: emp.name, 
                department: dep.name,
                depId: dep.department_id
            });
        }
    }
    return innerJoin;
}

Use a Map

Existing answers are using Object.groupBy to create a map of departments grouped by id.

This is inappropriate as there is only a one to one relationship between department id and departments. Object.groupBy creates groups using arrays to hold members of each group. One to many.

Using a more appropriate Map to get departments by id will simplify the code a little as there is no need to do the additional array indexing when locating a department.

Don`t duplicate data

No can I see why you duplicate the department_id as employee_department_id which is redundant information.

const employees = [{name: 'Alice', department_id: 12}, {name: 'Bob', department_id: 13}, {name: 'Chris', department_id: 13}, {name: 'Dan', department_id: 14}, {name: 'Eve', department_id: null}];
const departments = [{department_id: 12, name: 'Sales'}, {department_id: 13, name: 'Marketing'}, {department_id: 14, name: 'Engineering'}, {department_id: 15, name: 'Accounting'}, {department_id: 16, name: 'Operations'}];

console.log(innerJoinEmpDep(departments, employees));

function innerJoinEmpDep(deps, emps) {
    const depsById = new Map(deps.map(dep=> ([dep.department_id, dep])));
    const innerJoin = [];
    for (const emp of emps) {
        const dep = depsById.get(emp.department_id);
        if (dep) {
            innerJoin.push({
                employ: emp.name, 
                department: dep.name,
                depId: dep.department_id
            });
        }
    }
    return innerJoin;
}

Use a Map

Existing answers are using Object.groupBy to create a map of departments grouped by id.

This is inappropriate as there is only a one to one relationship between department id and departments. Object.groupBy creates groups using arrays to hold members of each group. One to many.

Using a more appropriate Map to get departments by id will simplify the code a little as there is no need to do the additional array indexing when locating a department.

Don't duplicate data

Nor can I see why you duplicate the department_id as employee_department_id which is redundant information.

const employees = [{name: 'Alice', department_id: 12}, {name: 'Bob', department_id: 13}, {name: 'Chris', department_id: 13}, {name: 'Dan', department_id: 14}, {name: 'Eve', department_id: null}];
const departments = [{department_id: 12, name: 'Sales'}, {department_id: 13, name: 'Marketing'}, {department_id: 14, name: 'Engineering'}, {department_id: 15, name: 'Accounting'}, {department_id: 16, name: 'Operations'}];

console.log(innerJoinEmpDep(departments, employees));

function innerJoinEmpDep(deps, emps) {
    const depsById = new Map(deps.map(dep=> ([dep.department_id, dep])));
    const innerJoin = [];
    for (const emp of emps) {
        const dep = depsById.get(emp.department_id);
        if (dep) {
            innerJoin.push({
                employ: emp.name, 
                department: dep.name,
                depId: dep.department_id
            });
        }
    }
    return innerJoin;
}

Source Link
Blindman67
  • 22.9k
  • 2
  • 17
  • 40

Use a Map

Existing answers are using Object.groupBy to create a map of departments grouped by id.

This is inappropriate as there is only a one to one relationship between department id and departments. Object.groupBy creates groups using arrays to hold members of each group. One to many.

Using a more appropriate Map to get departments by id will simplify the code a little as there is no need to do the additional array indexing when locating a department.

Don`t duplicate data

No can I see why you duplicate the department_id as employee_department_id which is redundant information.

const employees = [{name: 'Alice', department_id: 12}, {name: 'Bob', department_id: 13}, {name: 'Chris', department_id: 13}, {name: 'Dan', department_id: 14}, {name: 'Eve', department_id: null}];
const departments = [{department_id: 12, name: 'Sales'}, {department_id: 13, name: 'Marketing'}, {department_id: 14, name: 'Engineering'}, {department_id: 15, name: 'Accounting'}, {department_id: 16, name: 'Operations'}];

console.log(innerJoinEmpDep(departments, employees));

function innerJoinEmpDep(deps, emps) {
    const depsById = new Map(deps.map(dep=> ([dep.department_id, dep])));
    const innerJoin = [];
    for (const emp of emps) {
        const dep = depsById.get(emp.department_id);
        if (dep) {
            innerJoin.push({
                employ: emp.name, 
                department: dep.name,
                depId: dep.department_id
            });
        }
    }
    return innerJoin;
}