惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
小众软件
小众软件
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
C
Check Point Blog
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
罗磊的独立博客
有赞技术团队
有赞技术团队
V
V2EX
Y
Y Combinator Blog
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
Scott Helme
Scott Helme
云风的 BLOG
云风的 BLOG
Attack and Defense Labs
Attack and Defense Labs

博客园 - 努力&快乐

springboot 手动开启事务及手动提交事务 js MD5包含中文串时加密结果与JAVA结果不一致的解决方案 mongodb配置、启动、备份 linux系统关闭指定服务的方式 Dart 语言简易教程系列 查看java内存情况命令 JAVA用QRCode生成二维码 安装redis时Newer version of jemalloc required错误解决 代码指标工具 spring mvc接收ajax提交的JSON数据,并反序列化为对象 二进制样式的字符串与byte数组互转函数示例 .net运行时dll的查找路径顺序 WebStorm11 注册 MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型 多个App间传递数据 cordova混合开发:Android中native调用javascript cordova plugin数据传递概要 javascript不用new关键字创建对象示例 .Net事件管道详解图
JavaScript通过递归合并JSON
努力&快乐 · 2018-09-11 · via 博客园 - 努力&快乐

通过递归合并JSON:

 function mergeJSON(o, n) {
    let oType = Object.prototype.toString.call(o);
    let nType = Object.prototype.toString.call(n);
    if (nType == '[object Object]' && oType == '[object Object]') {
        //合并属性(object)
        for (let p in n) {
            if (n.hasOwnProperty(p) && !o.hasOwnProperty(p)) {
                o[p] = n[p];
            } else if (n.hasOwnProperty(p) && (o.hasOwnProperty(p))) {
                let oPType = Object.prototype.toString.call(o[p]);
                let nPType = Object.prototype.toString.call(n[p]);
                if ((nPType == '[object Object]' && oPType == '[object Object]') || (nPType == '[object Array]' && oPType == '[object Array]')) {
                    mergeJSON(o[p], n[p]);
                } else {
                    o[p] = n[p];
                }
            }
        }
    } else if (nType == '[object Array]' && oType == '[object Array]') {
        //合并属性(array)
        for (let i in n) {
            let oIType = Object.prototype.toString.call(o[i]);
            let nIType = Object.prototype.toString.call(n[i]);
            if ((nIType == '[object Object]' && oIType == '[object Object]') || (nIType == '[object Array]' && oIType == '[object Array]')) {
                mergeJSON(o[i], n[i]);
            } else {
                o[i] = n[i];
            }
        }
    }

    //合并属性(other)
    o = n;
}

//test

let json1={
    "a": "json1", 
    "b": 123, 
    "c": {
        "dd": "json1 dd", 
        "ee": 556, 
        "f": "ff1"
    }, 
    "list": [
        {
            "a": 123, 
            "b": 96
        }, 
        {
            "a": 45, 
            "b": 56
        }
    ]
};
let json2={

    "a": "json2", 
    "a2": 369, 
    "c": {
        "dd2": "json2 dd", 
        "ee": "gg2"
    }, 
    "list": [
        {
            "a": "a1", 
            "b1": 69
        }, 
        369, 
        {
            "i3": 36
        }
    ]
};

mergeJSON(json1,json2);

console.log(JSON.stringify(json1));