Joins in Javascript
javascript, join, lodash, underscore.js
Solution
This implementation uses the ES6 spread operator. Again, not a library function as asked for.
const leftJoin = (objArr1, objArr2, key1, key2) => objArr1.map(
anObj1 => ({
...objArr2.find(
anObj2 => anObj1[key1] === anObj2[key2]
),
...anObj1
})
);
Problem
I have 2 lists of objects: ``` people = [{id: 1, name: "Tom", carid: 1}, {id: 2, name: "Bob", carid: 1}, {id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}]; cars= [{id: 1, name: "Ford Fiesta", color: "blue"}, {id: 2, name: "Ferrari", color: "red"}, {id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}]; ``` Is there a function (possibly in Angular, JQuery, Underscore, LoDash, or other external library) to do a left join in one line on these? Something like: ``` peoplewithcars = leftjoin( people, cars, "carid", "id"); ``` I can write my own, but if LoDash has an optimised version I'd like to use that.