import { TObject } from "./dtypes"; export namespace dmarge { //Below is an example TypeScript function that takes two lists of objects and a key as input, and then merges the lists based on the value of the provided key. // Note that later fister item will be override by later items export function mergeListsByField( list1: TObject[], list2: TObject[], key: string ): TObject[] { const mergedList: TObject[] = []; const map: Map = new Map(); // Add objects from list1 to the map for (const obj of list1) { const keyValue = obj[key]; map.set(keyValue, obj); } // Add objects from list2 to the map (overwrite if duplicate key) for (const obj of list2) { const keyValue = obj[key]; map.set(keyValue, { ...map.get(keyValue), ...obj }); } // Convert the map values back to a list mergedList.push(...map.values()); return mergedList; } } // test (() => { // dlog.obj(dmarge.mergeListsByField([{ key: 1, v: 'a' }, { key: 3, 'j': 2 }], [{ key: 1, 'j': 2 }, { key: 2, 'j': 2 }], 'key')) })();