现有两个对象:
var obj1 = {
students: {
lingxuan: {
name: 'lingxuan',
addr: 'guangzhou'
}
}
}
var obj2 = {
students: {
lingxuan: {
mobile: '1823430****'
}
},
teachers: {
sam: {
name: 'sam'
}
}
}
我想通过deepMerge(obj1, obj2)
以后最终能得到(要用原生js):
newObj = {
students: {
lingxuan: {
name: 'lingxuan',
addr: 'guangzhou',
mobile: '1823430****'
}
},
teachers: {
sam: {
name: 'sam'
}
}
}
这个题目想了很久没想出来怎么解,也想过用递归和Object.assign
但是得不到想要的结果,希望大神指点指点。
感谢@小明同学的提醒,翻看了jQuery的源码有了思路,于是代码如下: