Is there some way I can "join" the contents of two javascript arrays much like I would do a join in SQL Is there some way I can "join" the contents of two javascript arrays much like I would do a join in SQL javascript javascript

Is there some way I can "join" the contents of two javascript arrays much like I would do a join in SQL


I think what you want is an inner join, which is simple enough to implement in JavaScript:

const innerJoin = (xs, ys, sel) =>    xs.reduce((zs, x) =>    ys.reduce((zs, y) =>        // cartesian product - all combinations    zs.concat(sel(x, y) || []), // filter out the rows and columns you want    zs), []);

For the purpose of demonstration we'll use the following data set (thank you @AshokDamani):

const userProfiles = [    {id: 1, name: "Ashok"},    {id: 2, name: "Amit"},    {id: 3, name: "Rajeev"},];const questions = [    {id: 1, text: "text1", createdBy: 2},    {id: 2, text: "text2", createdBy: 2},    {id: 3, text: "text3", createdBy: 1},    {id: 4, text: "text4", createdBy: 2},    {id: 5, text: "text5", createdBy: 3},    {id: 6, text: "text6", createdBy: 3},];

This is how you would use it:

const result = innerJoin(userProfiles, questions,    ({id: uid, name}, {id, text, createdBy}) =>        createdBy === uid && {id, text, name});

In SQL terms this would be similar to:

SELECT questions.id, questions.text, userProfiles.nameFROM userProfiles INNER JOIN questionsON questions.createdBy = userProfiles.id;

Putting it all together:

const innerJoin = (xs, ys, sel) =>    xs.reduce((zs, x) =>    ys.reduce((zs, y) =>        // cartesian product - all combinations    zs.concat(sel(x, y) || []), // filter out the rows and columns you want    zs), []);const userProfiles = [    {id: 1, name: "Ashok"},    {id: 2, name: "Amit"},    {id: 3, name: "Rajeev"},];const questions = [    {id: 1, text: "text1", createdBy: 2},    {id: 2, text: "text2", createdBy: 2},    {id: 3, text: "text3", createdBy: 1},    {id: 4, text: "text4", createdBy: 2},    {id: 5, text: "text5", createdBy: 3},    {id: 6, text: "text6", createdBy: 3},];const result = innerJoin(userProfiles, questions,    ({id: uid, name}, {id, text, createdBy}) =>        createdBy === uid && {id, text, name});console.log("Open your browser console to see the output.");console.table(result);