当前位置:网站首页>(JS) handwritten deep copy
(JS) handwritten deep copy
2022-06-29 11:11:00 【Yu Cainiao, who asked not to be named】
JS Hand written deep copy
const obj1 = {
age: 20,
name: "xxx",
address: {
city: "earth"
}
};
/** * * @param obj {Object} */
function deepClone(obj = {
}) {
// If obj Not an object or empty , Return an empty object directly
if (typeof obj !== "object" || obj == null) {
return obj;
}
let result;
// If obj It's the object , Then judge whether it is an array , If it's an array , Then for result Assign to array
if (obj instanceof Array) {
result = [];
} else {
result = {
};
}
// Traverse obj Properties of
for(let key in obj) {
// Judge whether it is obj Properties of itself , If so, use recursive assignment to result, It can also be used directly obj[key] assignment , The result remains unchanged.
if(obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
console.log(deepClone(obj1))
边栏推荐
- map合并相同的键,值合并为列表
- Pipeline aggregations管道聚合-Sibling-1
- MySQL get table information
- (JS)模仿indexOf方法寻找字符串中某个字符的位置
- Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique
- Multi thread communication between client and server (primary version)
- ZABBIX monitors various MySQL indicators
- 非凸联合创始人李佐凡:将量化作为自己的终身事业
- 直击产业落地!飞桨重磅推出业界首个模型选型工具
- Daily question brushing record (VII)
猜你喜欢

Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique

what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together

Online sql to htmltable tool

Modbustcp protocol network learning single channel infrared module (medium shell version)

当技术人成长为 CEO,应该修改哪些“Bug”?

Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)

How to obtain method parameter values through WinDbg

The last 48 hours! The cloud XR theme competition invites you to bloom together. See you at the competition!

加密市场接连爆雷,Celsius能避免破产吗?

多线程实现客户端与服务端通信(初级版本)
随机推荐
arcgis创建postgre企业级数据库
什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手
Pytorch learning notes (6) -- source code analysis of dataloader
Pipeline aggregations管道聚合- parent-2
Does anyone encounter this problem when flinkcdc synchronizes MySQL?
(JS)捕获错误(异常)
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
第12周实验---基于FPGA的VGA协议实现
Specific method and example program of Siemens s7-200smart control stepping motor
【数字信号调制】基于 AM+FM+DSB+SSB实现信号调制解调含Matlab源码
彻头彻尾理解JVM系列之七:对象在分代模型中的流转过程是怎样的?
(JS)数组方法:slice和splice
9 款好用到爆的 JSON 处理工具,极大提高效率!
【NLP】文本生成专题1:基础知识
Numeric Keypad
(JS)模仿indexOf方法寻找字符串中某个字符的位置
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
Nuc980 open source project 16- start from SPI flash (w25q128)
How to obtain method parameter values through WinDbg
宁德时代麒麟电池有着更大的野心