Files and data
Merge exported records by a stable key
Use a Map to apply later records as replacements for earlier records sharing the same ID. Decide whether an update replaces the whole record or merges individual fields before implementing the operation.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const oldRows = [{id: "a", count: 1, label: "old"}, {id: "b", count: 2}];
const updates = [{id: "a", count: 3}];
const records = new Map(oldRows.map(row => [row.id, row]));
for (const row of updates) records.set(row.id, row);
console.log(JSON.stringify([...records.values()]));How it works
The example uses whole-record replacement, so a’s old label disappears. Records not mentioned by an update remain. This is appropriate when an update file contains authoritative complete rows rather than partial patches.
Limits and mistakes
If updates contain partial fields, replacement can discard valid data. Conversely, shallow merging can preserve fields that should have been deleted. Missing IDs and duplicate updates also need a documented policy. A map does not resolve contradictory business rules.
Check the result
Expect a with count 3 and no label, followed by b with count 2. Test a partial update deliberately and confirm whether replacement matches the source format’s contract.
Sources
Reference documentation for the behavior used in this guide: