我是靠谱客的博主 朴实口红,这篇文章主要介绍【tree-3】在deepClone的过程中变更树的单个节点值,现在分享给大家,希望可以做个参考。

在深拷贝的过程中变更单个节点值

复制代码
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
function changeTree(source: Object, map = new Map()): any { //空、null和基本数据类型不用递归 if (source === null || source === {} || source === [] || typeof source !== 'object') { return source; } let newObject: any; let isArray = false; if (Array.isArray(source)) { newObject = []; isArray = true; } else { newObject = {}; isArray = false; } //防止循环引入 if (map.get(source)) { return map.get(source); } map.set(source, newObject); //数组和对象都有key,数组是下标 for (const key of Object.keys(source)) { const sub = typeof (source as any)[key] === 'object' ? changeTree((source as any)[key], map) : (source as any)[key]; if (isArray) { newObject.push(sub); } else { newObject[key] = sub; //trip if (key === 'xxx') { newObject.label = sub; } } } return newObject; }

最后

以上就是朴实口红最近收集整理的关于【tree-3】在deepClone的过程中变更树的单个节点值的全部内容,更多相关【tree-3】在deepClone内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部