October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Choose the Right TypeScript Object Array Merge for Your Goal

TypeScript array spread and concat append elements; object spread combines fields. To merge arrays by ID, choose duplicate, precedence, ordering, and missing-ID rules explicitly.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use concat() or array spread to append elements; use object spread to combine the fields of two records; and use an explicit lookup when matching records by id. These operations have different effects: neither array operation merges matching objects, and every spread or concatenation shown here makes only a shallow copy.

Choose the operation that matches your goal

Goal Use What it does
Append both arrays and keep duplicate elements first.concat(second) or [...first, ...second] Returns an array with the elements of first followed by those of second. It does not merge object fields. MDN documents that concat() returns a new array without changing its inputs; array spread expands iterable elements into an array literal.
Combine properties of two individual objects { ...first, ...second } Creates a shallow object copy; for a property present in both, the value from second wins.
Combine or deduplicate records with the same ID Explicit lookup, such as a Map Requires a chosen rule for duplicate IDs, conflicting fields, ordering, and missing or malformed IDs.

Append array elements with concat or spread

When the goal is simply to put every element into one array, either form works:

As an Amazon Associate I earn from qualifying purchases.

const combined = first.concat(second);
// Or:
const combinedWithSpread = [...first, ...second];

Both produce the first array’s elements followed by the second array’s elements, including any duplicate records. concat() creates a new shallow array and leaves the input arrays unchanged. Array spread also creates a new array literal by expanding the elements of each iterable; it does not inspect object IDs or combine object properties.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Combine properties of two individual records

For two records that should become one object, object spread copies their enumerable own properties into a new object:

const merged = { ...existing, ...incoming };

Here, incoming takes precedence wherever both objects have the same property. To make the existing record win instead, reverse the order: { ...incoming, ...existing }. This is a top-level property merge, not a recursive merge of nested objects. The TypeScript 2.1 release notes document object spread for shallow copying and combining properties; that historical feature note is not a compatibility matrix for every current compiler target.

Merge arrays by ID with an explicit policy

Neither concat() nor array spread decides what counts as a matching record. The example below chooses one particular policy: retain the order in which IDs first appear, merge top-level fields for matching IDs, and let the second array win when fields conflict.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
type Item = { id: string; [key: string]: unknown };

function mergeById(first: Item[], second: Item[]): Item[] {
  const byId = new Map<string, Item>();

  for (const item of [...first, ...second]) {
    const previous = byId.get(item.id);
    byId.set(item.id, previous ? { ...previous, ...item } : { ...item });
  }

  return [...byId.values()];
}

As the loop visits the first array and then the second, a new ID is inserted at its first occurrence. A later record with that ID replaces the map’s value without adding another output entry; spreading the previous record before the current one means the later record wins on conflicts. The returned values therefore appear in first-seen ID order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is an illustrative policy, not a universal rule. If a later record should replace the entire earlier record, store { ...item } for every occurrence instead of spreading the previous value into it. If IDs can be absent, non-string, or case-insensitive, define and type that behavior explicitly rather than assuming the sample covers it.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Understand what stays shared

These operations are shallow. They create new outer arrays or objects, but do not recursively clone nested values. If a record contains a nested object or array, the copied record can still reference that same nested value. For nested properties, define whether sharing is acceptable or implement the required copying separately; do not assume array or object spread makes the result deeply independent. MDN describes these built-in copying operations as shallow in its documentation for concat(), spread syntax, and arrays.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.