deepClone — Deep Clone
17:43Description
$u.deepClone copies an object or array through JSON serialization and deserialization, returning a copy with the same content but a different reference; modifying the copy does not affect the original.
Examples
Object deep clone
Clones a nested object on click and checks that the copy has a different reference yet identical JSON.stringify content.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item title="原对象" :label="JSON.stringify(originalObj)" :arrow="false" :border-bottom="false" />
</ux-cell-group>
<ux-gap :height="8" />
<ux-button size="small" type="primary" block text="克隆对象" @click="handleClone()" />
<template v-if="clonedObj != null">
<ux-gap :height="8" />
<ux-cell-group :border="false">
<ux-cell-item title="克隆对象" :label="JSON.stringify(clonedObj)" :arrow="false" />
<ux-cell-item title="是否同一引用" :arrow="false">
<ux-tag size="mini" :type="isSameRef ? 'error' : 'success'" :text="isSameRef ? '是' : '否'" />
</ux-cell-item>
<ux-cell-item title="内容是否相等" :arrow="false" :border-bottom="false">
<ux-tag size="mini" :type="isDeepEqual ? 'success' : 'error'" :text="isDeepEqual ? '是' : '否'" />
</ux-cell-item>
</ux-cell-group>
</template>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
const originalObj = ref<JsonObject>({
name: '张三',
age: 25,
address: { city: '北京', district: '朝阳区' }
});
const clonedObj = ref<JsonObject | null>(null);
const isSameRef = ref<boolean>(false);
const isDeepEqual = ref<boolean>(false);
function handleClone(): void {
clonedObj.value = $u.deepClone(originalObj.value);
isSameRef.value = originalObj.value === clonedObj.value;
isDeepEqual.value = JSON.stringify(originalObj.value) === JSON.stringify(clonedObj.value);
}
</script>Array deep clone
Clones an array containing primitives and a nested object, showing that the copy's JSON content matches the original.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item title="原数组" :label="JSON.stringify(originalArr)" :arrow="false" :border-bottom="false" />
</ux-cell-group>
<ux-gap :height="8" />
<ux-button size="small" type="primary" block text="克隆数组" @click="handleCloneArray()" />
<template v-if="clonedArr.length > 0">
<ux-gap :height="8" />
<ux-cell-group :border="false">
<ux-cell-item title="克隆数组" :label="JSON.stringify(clonedArr)" :arrow="false" :border-bottom="false" />
</ux-cell-group>
</template>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
const originalArr = ref<any[]>([1, 2, { a: 3 }]);
const clonedArr = ref<any[]>([]);
function handleCloneArray(): void {
clonedArr.value = $u.deepClone(originalArr.value);
}
</script>API
Parameters
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| obj | The object or array to clone | T | - | - |
Returns
| Type | Description |
|---|---|
T | A new copy with the same type as the input |
Notes
- The implementation is
JSON.parse(JSON.stringify(obj)), so only JSON-serializable data is copied correctly; functions andundefinedare lost. Dateis serialized into an ISO string, and special objects such asRegExp,MapandSetdo not keep their original type.- Circular references make
JSON.stringifythrow. - The result is a brand-new reference, so modifying the copy does not affect the original object or array.