js深合并
作者:互联网
function deepMerge(obj1, obj2) { let key; for (key in obj2) { // 如果target(也就是obj1[key])存在,且是对象的话再去调用deepMerge,否则就是obj1[key]里面没这个对象,需要与obj2[key]合并 // 如果obj2[key]没有值或者值不是对象,此时直接替换obj1[key] obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" && (obj2[key] && obj2[key].toString() === "[object Object]") ? deepMerge(obj1[key], obj2[key]) : (obj1[key] = obj2[key]); } return obj1; }
--
标签:obj1,obj2,key,Object,合并,js,deepMerge,&& 来源: https://www.cnblogs.com/fqh123/p/15761849.html