Skip to content

deepMerge 深合并 ​

17:43

说明 ​

$u.deepMerge 递归合并两个 JsonObject:source 的同名字段覆盖 target,两侧都是普通对象时继续向下合并,数组则按整体替换。

示例 ​

基础对象合并 ​

source 的 age、email 覆盖或新增到 target,address 递归合并后同时包含两边的字段。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="目标对象" :label="JSON.stringify(targetObj)" :arrow="false" />
        <ux-cell-item title="源对象" :label="JSON.stringify(sourceObj)" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
    <ux-gap :height="8" />
    <ux-button size="small" type="primary" block text="执行合并" @click="handleMerge()" />
    <template v-if="mergedObj != null">
        <ux-gap :height="8" />
        <ux-cell-group :border="false">
            <ux-cell-item title="合并结果" :label="JSON.stringify(mergedObj)" :arrow="false" :border-bottom="false" />
        </ux-cell-group>
    </template>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

const targetObj = ref<JsonObject>({
    name: '张三',
    age: 25,
    address: { city: '北京', district: '朝阳区' }
});
const sourceObj = ref<JsonObject>({
    age: 30,
    email: 'zhangsan@example.com',
    address: { city: '上海', street: '南京路' }
});
const mergedObj = ref<JsonObject | null>(null);

function handleMerge(): void {
    mergedObj.value = $u.deepMerge(targetObj.value, sourceObj.value);
}
</script>

数组属性合并 ​

items 两侧都是数组,按整体替换为 source 的 [3, 4],不做逐项合并。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="目标" :value="JSON.stringify(targetArr)" :arrow="false" />
        <ux-cell-item title="源" :value="JSON.stringify(sourceArr)" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
    <ux-gap :height="8" />
    <ux-button size="small" type="primary" block text="合并数组" @click="handleMergeArray()" />
    <template v-if="mergedArr != null">
        <ux-gap :height="8" />
        <ux-cell-group :border="false">
            <ux-cell-item title="结果" :value="JSON.stringify(mergedArr)" :arrow="false" :border-bottom="false" />
        </ux-cell-group>
    </template>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

const targetArr = ref<JsonObject>({ items: [1, 2] });
const sourceArr = ref<JsonObject>({ items: [3, 4] });
const mergedArr = ref<JsonObject | null>(null);

function handleMergeArray(): void {
    mergedArr.value = $u.deepMerge(targetArr.value, sourceArr.value);
}
</script>

深层嵌套合并 ​

三层嵌套的 user.profile.settings 逐层合并,保留 theme 并新增 language。

查看源码
vue
<template>
    <ux-cell-group :border="false">
        <ux-cell-item title="目标" :label="JSON.stringify(complexTarget)" :arrow="false" />
        <ux-cell-item title="源" :label="JSON.stringify(complexSource)" :arrow="false" :border-bottom="false" />
    </ux-cell-group>
    <ux-gap :height="8" />
    <ux-button size="small" type="primary" block text="嵌套合并" @click="handleComplexMerge()" />
    <template v-if="complexMerged != null">
        <ux-gap :height="8" />
        <ux-cell-group :border="false">
            <ux-cell-item title="结果" :label="JSON.stringify(complexMerged)" :arrow="false" :border-bottom="false" />
        </ux-cell-group>
    </template>
</template>

<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';

const complexTarget = ref<JsonObject>({
    user: { profile: { name: '张三', settings: { theme: 'light' } } }
});
const complexSource = ref<JsonObject>({
    user: { profile: { settings: { language: 'zh-CN' } } }
});
const complexMerged = ref<JsonObject | null>(null);

function handleComplexMerge(): void {
    complexMerged.value = $u.deepMerge(complexTarget.value, complexSource.value);
}
</script>

API ​

参数 ​

参数说明类型默认值版本
target目标对象,优先级较低JsonObject--
source来源对象,同名字段覆盖目标JsonObject--

返回值 ​

类型说明
JsonObject合并后的新对象,入参本身不会被修改

注意事项 ​

  • 只处理 JsonObject;source 的同名字段覆盖 target,两侧同为普通对象时才递归合并。
  • 数组按整体替换,不做逐项合并。
  • 返回的是新的顶层对象,但 source 未覆盖的嵌套字段仍与 target 共享引用。
  • 传入 null 或非对象值不在签名支持范围内,调用前应自行保证入参类型。