deepMerge 【递归深合并对象】
描述
深度(递归)合并对象 v3.0.6+,主要利用deepmerge (opens new window)
# 1.示例
import { deepMerge } from 'sf-utils2'
const x = {
foo: { bar: 3 },
array: [
{
does: 'work',
too: [1, 2, 3]
}
]
}
const y = {
foo: { baz: 4 },
quux: 5,
array: [
{
does: 'work',
too: [4, 5, 6]
},
{
really: 'yes'
}
]
}
deepMerge(x, y) //
// 输出 output
const output = {
foo: {
bar: 3,
baz: 4
},
array: [
{
does: 'work',
too: [1, 2, 3]
},
{
does: 'work',
too: [4, 5, 6]
},
{
really: 'yes'
}
],
quux: 5
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 2.入参说明
| 参数 | 说明 | 类型 | 是否必填 |
|---|---|---|---|
| target | 目标对象 | Object、Array | -------- |
| source | 原对象 | Object、Array | -------- |
# 3.源码
import _helperDeepMerge from '@/_helper/_helperDeepMerge'
/**
* 深合并对象
* @param {Object|Array} target 目标对象
* @param {Object|Array} source 原对象
* @return {Object|Array|any}
*/
function deepMerge(target, source) {
return _helperDeepMerge(target, source)
}
export default deepMerge
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2023/06/24, 19:35:48