Skip to content

Instantly share code, notes, and snippets.

@XoseLluis
Last active December 19, 2020 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XoseLluis/d7109b7ef040dd72b16f5314fa751dd5 to your computer and use it in GitHub Desktop.
Save XoseLluis/d7109b7ef040dd72b16f5314fa751dd5 to your computer and use it in GitHub Desktop.
Join JavaScript arrays (like a DB join)
//sort of inner join
//improved version where we directly obtain the alias from the parameter name in the selector functions
function joinCollectionsImproved(collection1, collection2, idSelectorFn1, idSelectorFn2){
let getAlias = selectorFn => selectorFn.toString().split("=>")[0].trim();
let [alias1, alias2] = [idSelectorFn1, idSelectorFn2].map(getAlias);
let result = [];
collection1.forEach(it1 => {
let id1 = idSelectorFn1(it1);
collection2.forEach(it2 =>{
if (id1 === idSelectorFn2(it2))
result.push({
[alias1]: it1,
[alias2]: it2
});sa
});
});
return result;
}
//old version where we were providing the alias as parameters
function joinCollections_old(collection1, collection2, idSelectorFn1, idSelectorFn2, alias1, alias2){
let result = [];
collection1.forEach(it1 => {
let id1 = idSelectorFn1(it1);
collection2.forEach(it2 =>{
if (id1 === idSelectorFn2(it2))
result.push({
[alias1]: it1,
[alias2]: it2
});
});
});
return result;
}
let joinCollections = joinCollectionsImproved;
let employees = [
{
firstName: "Terry",
lastName: "Adams",
role: "FrontEnd Developer"
},
{
firstName:"Charlotte",
lastName:"Weiss",
role: "Systems Administrator"
},
{
firstName:"Magnus",
lastName:"Hedland",
role: "Psycologist"
},
{
firstName:"Vernette",
lastName:"Price",
role: "Shop Assistant"
}
];
var students = [
{
firstName:"Vernette",
lastName:"Price",
studies: "Philosophy"
},
{
firstName:"Terry",
lastName:"Earls",
studies: "Computer Engineering"
},
{
firstName:"Terry",
lastName:"Adams",
studies: "Computer Engineering"
}
];
let salaries = [
{
job: "FrontEnd Developer",
amount: 40000
},
{
job: "Shop Assistant",
amount: 20000
}
];
let persons = joinCollections(employees,
students,
employee => `${employee.firstName}-${employee.lastName}`,
student => `${student.firstName}-${student.lastName}`,
"employee",
"student"
).map(ob => {
return {
fullName: `${ob.employee.firstName} ${ob.employee.lastName}`,
job: ob.employee.role,
studies: ob.student.studies
};
});
console.log("-- persons:\n " + JSON.stringify(persons, null, "\t"));
let persons2 = joinCollections(employees,
students,
employee => `${employee.firstName}-${employee.lastName}`,
student => `${student.firstName}-${student.lastName}`,
"employee",
"student"
)
.filter(ob => ob.employee.role === "FrontEnd Developer")
.map(ob => {
return {
fullName: `${ob.employee.firstName} ${ob.employee.lastName}`,
job: ob.employee.role,
studies: ob.student.studies
};
});
console.log("-- persons2:\n " + JSON.stringify(persons2, null, "\t"));
let personsWithSalaries = joinCollections(
persons,
salaries,
person => person.job,
salary => salary.job,
"person",
"salary"
).map(ob => {
return {...ob.person, ...ob.salary};
});
console.log("-- personsWithSalaries:\n " + JSON.stringify(personsWithSalaries, null, "\t"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment