当前位置:网站首页>Deep merge object deep copy of vant source code parsing
Deep merge object deep copy of vant source code parsing
2022-07-05 20:58:00 【The legend of Feng】
Source code
Merge the properties of two objects , Deep objects can also
function isDef(value: any): boolean {
// value:any Indicates that the parameter passed in is of any type boolean Indicates that the return value is boolean type
return value !== undefined && value !== null;
// The value passed in cannot be undefined Nor is null
}
// in 15.686s 0.486s
function isObj(x: any): boolean {
debugger
// Judge whether a value is object type
// Incoming parameter yes any Any type i
// The return value is boolean
const type = typeof x;
// typeof Judge data type
// typeof When judging the data type null It's also object therefore type Not for null
// The function is also object
return x !== null && (type === 'object' || type === 'function');
}
const { hasOwnProperty } = Object.prototype;
// Determine whether an object contains an attribute ( Does not include attributes on the object prototype )
type objectType = {
[key: string]: any;
}
function assignKey(to: objectType, from: objectType, key: string) {
const val = from[key];
// Get the old data Some key Value
if (!isDef(val)) {// At present key Corresponding Value of old data yes undefined Just return
return;
}
if (!hasOwnProperty.call(to, key) || !isObj(val)) {
// If the target object's This key If it doesn't exist The old data corresponds to key value Assign a value to Target audience to
to[key] = val;
} else {
to[key] = deepAssign(Object(to[key]), from[key]);
}
}
function deepAssign(to: objectType, from: objectType) {
Object.keys(from).forEach(key => {
assignKey(to, from, key);
});
return to;
}
test
let toInfo={
name:"123",
age:'16',
more:{
hobby:' To play basketball ',
address:' Hefei '
},
color:'red'
}
let fromInfo={
name:"12343553",
age:'16545353',
more:{
hobby:' To play basketball 5353',
address:' Hefei 5353'
},
sex:' male '
}
let result: objectType = deepAssign(toInfo,fromInfo)
console.log("result",result)
Deep copy
import { deepAssign } from './deep-assign';
export function deepClone(obj: object): object {
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}
if (typeof obj === 'object') {
return deepAssign({}, obj);
}
return obj;
}
边栏推荐
- LeetCode_哈希表_困难_149. 直线上最多的点数
- 显示屏DIN 4102-1 Class B1防火测试要求
- ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
- Is Kai Niu 2980 useful? Is it safe to open an account
- 概率论机器学习的先验知识(上)
- 当Steam教育进入个性化信息技术课程
- 示波器探头对测量带宽的影响
- Is the securities account given by the school of Finance and business safe? Can I open an account?
- When steam education enters personalized information technology courses
- Analyze the knowledge transfer and sharing spirit of maker Education
猜你喜欢
MySQL fully parses json/ arrays
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
解析五育融合之下的steam教育模式
Duchefa p1001 plant agar Chinese and English instructions
示波器探头对信号源阻抗的影响
leetcode:1139. 最大的以 1 为边界的正方形
CLion配置visual studio(msvc)和JOM多核编译
木板ISO 5660-1 热量释放速率摸底测试
PHP反序列化+MD5碰撞
Clion-MinGW编译后的exe文件添加ico图标
随机推荐
How to renew NPDP? Here comes the operation guide!
phpstudy小皮的mysql点击启动后迅速闪退,已解决
LeetCode: Distinct Subsequences [115]
Duchefa low melting point agarose PPC Chinese and English instructions
启牛2980有没有用?开户安全吗、
Comparison table of foreign lead American abbreviations
Monorepo management methodology and dependency security
vant 源码解析之 utils/index.ts 工具函数
Influence of oscilloscope probe on measurement bandwidth
ts 之 泛型
When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
Prosci LAG-3 recombinant protein specification
systemd-resolved 开启 debug 日志
ViewRootImpl和WindowManagerService笔记
XML建模
Interpreting the daily application functions of cooperative robots
重上吹麻滩——段芝堂创始人翟立冬游记
Monorepo管理方法论和依赖安全
PVC 塑料片BS 476-6 火焰传播性能测定
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解