Skip to content

merge

ソースオブジェクトをターゲットオブジェクトに深くマージしてターゲットオブジェクトを修正します。

typescript
const result = merge(target, source);

参照

merge(target, source)

2つのオブジェクトを深くマージしたい時にmergeを使用してください。ネストされたオブジェクトと配列も再帰的にマージされます。toMergedとは異なり、元のtargetオブジェクトが修正されます。

typescript
import { merge } from 'es-toolkit/object';

// 基本的なオブジェクトマージ
const target = { a: 1, b: { x: 1, y: 2 } };
const source = { b: { y: 3, z: 4 }, c: 5 };
const result = merge(target, source);
// resultとtargetの両方が{ a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }になります

// 配列もマージされます
const arrayTarget = { a: [1, 2], b: { x: 1 } };
const arraySource = { a: [3], b: { y: 2 } };
merge(arrayTarget, arraySource);
// arrayTargetは{ a: [3, 2], b: { x: 1, y: 2 } }になります

// null値も適切に処理します
const nullTarget = { a: null };
const nullSource = { a: [1, 2, 3] };
merge(nullTarget, nullSource);
// nullTargetは{ a: [1, 2, 3] }になります

undefined値は既存の値を上書きしません。

typescript
const target = { a: 1, b: 2 };
const source = { b: undefined, c: 3 };
merge(target, source);
// targetは{ a: 1, b: 2, c: 3 }になります (bは上書きされません)

パラメータ

  • target (T extends Record<PropertyKey, any>): ソースオブジェクトをマージするターゲットオブジェクトです。このオブジェクトが修正されます。
  • source (S extends Record<PropertyKey, any>): ターゲットオブジェクトにマージするソースオブジェクトです。

戻り値

(T & S): ソースオブジェクトがマージされたターゲットオブジェクトを返します。

typescript
const target = { a: 1, b: { x: 1, y: 2 } };
const source = { b: { y: 3, z: 4 }, c: 5 };
const result = merge(target, source);
console.log(result);
// 戻り値: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }

const target = { a: [1, 2], b: { x: 1 } };
const source = { a: [3], b: { y: 2 } };
const result = merge(target, source);
console.log(result);
// 戻り値: { a: [3, 2], b: { x: 1, y: 2 } }

const target = { a: null };
const source = { a: [1, 2, 3] };
const result = merge(target, source);
console.log(result);
// 戻り値: { a: [1, 2, 3] }

デモ

import "./styles.css";

document.getElementById("app").innerHTML = `
<h1>Hello world</h1>
`;

パフォーマンス比較

バンドルサイズランタイムパフォーマンス
es-toolkit271 bytes (97.8% smaller)1,952,436 times (3.65× faster)
es-toolkit/compat4,381 bytes (64.9% smaller)706,558 times (1.32× faster)
lodash-es12,483 bytes533,484 times

MIT ライセンスの下で配布されています。