Left join two lists
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
For each row of `left`, in order, return a new object combining it with the first row of `right` that has the same `row[key]` (fields from `right` win when both have them). A left row without a match is returned as a copy. Keys match only when they are the same value, so `1` does not match `"1"`. Do not change either list.
What this exercise teaches
The Map turns the lookup into one step per left row instead of a scan of right, and Map keys keep their type. Only setting a key the first time keeps the first match. Spreading into new objects leaves both inputs untouched.
A hint if you get stuck
Index `right` once in a `Map` from key to the first row with that key (skip keys you already have). Then map `left` to `{ ...l, ...match }` or `{ ...l }`.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.