多层嵌套JSON对象无数据丢失合并实现方案技术问询
多层级JSON对象合并需求
我需要合并两个多层级JSON对象,生成无数据丢失的单一对象。合并规则为:层级数据完全一致时不新增额外字段,同一层级字段存在差异时,将新对象插入对应数组中。
我们需要将字段相似的JSON对象分组整合为一个完整JSON,对多层级对象合并以得到聚合结果。
示例输入对象
let one = { "name": "Hindi", "country": "USA", "region": "EAST COAST", "enttity": "ENTITY-2", "operation": [ { "p_name": "RECONSTRUCTION", "code": "jhhkj-132", "class": "medical", "products": [ { "main": "electronic", "area": [ { "subarea": "electrical", "use": 0, "max_use": 0, "mode_use": "10", "median": "12", "things": [ { "id": "1000514", "number": "1588TB-1", "description": "DESCRIPTION1", "manufacturer": "LG", "tag": "NOT AVAILABLE" } ] } ] } ] } ] }; let two = { "name": "Hindi", "country": "USA", "region": "EAST COAST", "enttity": "ENTITY-2", "operation": [ { "p_name": "RECONSTRUCTION", "code": "jhhkj-132", "class": "medical", "products": [ { "main": "electronic", "area": [ { "subarea": "mechanical", "use": 0, "max_use": 0, "mode_use": "10", "median": "12", "things": [ { "id": "1000523314", "number": "1588T3B-1", "description": "DES32CRIPTION1", "manufacturer": "LG", "tag": "NOT AVAILABLE" } ] }, { "subarea": "electrical", "use": 0, "max_use": 0, "mode_use": "10", "median": "12", "things": [ { "id": "1000514", "number": "1588TB-1", "description": "DESCRIPTION1", "manufacturer": "LG", "tag": "NOT AVAILABLE" } ] } ] } ] } ] };
期望输出结果
let output = { "name": "Hindi", "country": "USA", "region": "EAST COAST", "enttity": "ENTITY-2", "operation": [ { "p_name": "RECONSTRUCTION", "code": "jhhkj-132", "class": "medical", "products": [ { "main": "electronic", "area": [ { "subarea": "mechanical", "use": 0, "max_use": 0, "mode_use": "10", "median": "12", "things": [ { "id": "1000523314", "number": "1588T3B-1", "description": "DES32CRIPTION1", "manufacturer": "LG", "tag": "NOT AVAILABLE" } ] }, { "subarea": "electrical", "use": 0, "max_use": 0, "mode_use": "10", "median": "12", "things": [ { "id": "1000514", "number": "1588TB-1", "description": "DESCRIPTION1", "manufacturer": "LG", "tag": "NOT AVAILABLE" } ] } ] } ] } ] };
内容的提问来源于stack exchange,提问作者shashank yadav
相关产品推荐
相关产品推荐

