当前位置:网站首页>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;
}
边栏推荐
- Monorepo management methodology and dependency security
- Is it necessary for bazel to learn
- Typhoon is coming! How to prevent typhoons on construction sites!
- 基於flask寫一個接口
- Binary search
- 获取前一天的js(时间戳转换)
- Abnova丨血液总核酸纯化试剂盒预装相关说明书
- Learning notes of SAS programming and data mining business case 19
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- Material design component - use bottomsheet to show extended content (II)
猜你喜欢
基于AVFoundation实现视频录制的两种方式
The development of research tourism practical education helps the development of cultural tourism industry
CLion配置visual studio(msvc)和JOM多核编译
leetcode:1139. 最大的以 1 为边界的正方形
When steam education enters personalized information technology courses
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Influence of oscilloscope probe on signal source impedance
Abnova maxpab mouse derived polyclonal antibody solution
重上吹麻滩——段芝堂创始人翟立冬游记
Talk about my fate with some programming languages
随机推荐
ts 之 泛型
AITM2-0002 12s或60s垂直燃烧试验
Duchefa p1001 plant agar Chinese and English instructions
Duchefa d5124 md5a medium Chinese and English instructions
Monorepo管理方法论和依赖安全
CareerCup它1.8 串移包括问题
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Determine the best implementation of horizontal and vertical screens
ts 之 属性的修饰符public、private、protect
systemd-resolved 开启 debug 日志
ClickHouse 复制粘贴多行sql语句报错
Comparison table of foreign lead American abbreviations
Interpreting the daily application functions of cooperative robots
渗透创客精神文化转化的创客教育
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Chemical properties and application instructions of prosci Lag3 antibody
leetcode:1755. 最接近目标值的子序列和
phpstudy小皮的mysql点击启动后迅速闪退,已解决
PHP deserialization +md5 collision
珍爱网微服务底层框架演进从开源组件封装到自研