2722. Join Two Arrays by ID¶ TypeScript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31type JSONValue = | {} | null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }; type Item = { [key: string]: JSONValue } & { id: number; }; function join(arr1: Item[], arr2: Item[]): Item[] { const idToObj: { [key: number]: Item } = {}; for (const item of arr1) { idToObj[item.id] = item; } for (const item of arr2) { if (idToObj[item.id] === undefined) { idToObj[item.id] = item; } else { for (const key of Object.keys(item)) { idToObj[item.id][key] = item[key]; } } } const ans = Object.values(idToObj); ans.sort((a, b) => a.id - b.id); return ans; }