Node.js中如何合并两个复杂对象详解

前言

相信大家都知道在通常情况下,在Node.js中我们可以通过的extend或者的merge来合并两个对象,但是对于像下面这种复杂的对象,要如何来应对呢?下面来一起学习学习吧。

Node.js合并两个复杂对象

例如我有以下两个object:

rush:js;"> var obj1 = { "name" : "myname","status" : 0,"profile": { "sex":"m","isactive" : true},"strarr":["one","three"],"objarray": [ { "id": 1,"email": "a1@me.com","isactive":true },{ "id": 2,"email": "a2@me.com","isactive":false } ] };

var obj2 = {
"name" : "myname","status" : 1,"newfield": 1,"profile": { "isactive" : false,"city": "new York"},"strarr":["two"],"isactive":false
},"email": "a2modified@me.com"
},{
"id": 3,"email": "a3new@me.com","isactive" : true
}
]
};

希望合并之后的结果输出成下面这样:

rush:js;"> { name: 'myname',status: 1,profile: { sex: 'm',isactive: false,city: 'new York' },strarr: [ 'one','three','two' ],objarray: [ { id: 1,email: 'a1@me.com',isactive: false },{ id: 2,email: 'a2modified@me.com',{ id: 3,email: 'a3new@me.com',isactive: true } ],newfield: 1 }

通过或者现有的方法我们无法实现上述结果,那只能自己写代码来实现了。

for (var i in obj) {
// if its an object
if (obj[i] != null && obj[i].constructor == Object)
{
def[i] = mergeObjs(def[i],obj[i]);
}
// if its an array,simple values need to be joined. Object values need to be remerged.
else if(obj[i] != null && (obj[i] instanceof Array) && obj[i].length > 0)
{
// test to see if the first element is an object or not so we kNow the type of array we're dealing with.
if(obj[i][0].constructor == Object)
{
var newobjs = [];
// create an index of all the existing object IDs for quick access. There is no way to kNow how many items will be in the arrays.
var objids = {}
for(var x= 0,l= def[i].length ; x < l; x++ )
{
objids[def[i][x].id] = x;
}

// Now walk through the objects in the new array
// if the ID exists,then merge the objects.
// if the ID does not exist,push to the end of the def array
for(var x= 0,l= obj[i].length; x < l; x++)
{
var newobj = obj[i][x];
if(objids[newobj.id] !== undefined)
{
def[i][x] = mergeObjs(def[i][x],newobj);
}
else {
newobjs.push(newobj);
}
}

for(var x= 0,l = newobjs.length; x<l; x++) {
def[i].push(newobjs[x]);
}
}
else {
for(var x=0; x < obj[i].length; x++)
{
var idxObj = obj[i][x];
if(def[i].indexOf(idxObj) === -1) {
def[i].push(idxObj);
}
}
}
}
else
{
def[i] = obj[i];
}
}
return def;}

将上述代码稍作改进,我们可以实现在合并过程中将Number类型的值自动相加。

for (var i in obj) {
// if its an object
if (obj[i] != null && obj[i].constructor == Object)
{
def[i] = merge(def[i],obj[i]);
}
// if its an array,simple values need to be joined. Object values need to be re-merged.
else if(obj[i] != null && (obj[i] instanceof Array) && obj[i].length > 0)
{
// test to see if the first element is an object or not so we kNow the type of array we're dealing with.
if(obj[i][0].constructor == Object)
{
var newobjs = [];
// create an index of all the existing object IDs for quick access. There is no way to kNow how many items will be in the arrays.
var objids = {}
for(var x= 0,l= def[i].length ; x < l; x++ )
{
objids[def[i][x].id] = x;
}

// <a href="https://www.jb51.cc/tag/Now/" target="_blank" class="keywords">Now</a> walk through the objects in the new array
// if the ID exists,then merge the objects.
// if the ID does not exist,push to the end of the def array
for(var x= 0,l= obj[i].length; x < l; x++)
{
 var newobj = obj[i][x];
 if(objids[newobj.id] !== undefined)
 {
  def[i][x] = merge(def[i][x],newobj);
 }
 else {
  newobjs.push(newobj);
 }
}

for(var x= 0,l = newobjs.length; x<l; x++) {
 def[i].push(newobjs[x]);
}

}
else {
for(var x=0; x < obj[i].length; x++)
{
var idxObj = obj[i][x];
if(def[i].indexOf(idxObj) === -1) {
def[i].push(idxObj);
}
}
}
}
else
{
if (isNaN(obj[i]) || i.indexOf('_key') > -1){
def[i] = obj[i];
}
else{
def[i] += obj[i];
}
}
}
return def;
}

例如有以下两个对象:

rush:js;"> var data1 = { "_id" : "577327c544bd90be508b46cc","channelId_info" : [ { "channelId_key" : "0","secondLevel_group" : [ { "secondLevel_key" : "568cc36c44bd90625a045c60","sender_group" : [ { "sender_key" : "577327c544bd90be508b46cd","sender_sum" : 40.0 } ],"senders_sum" : 40.0 } ],"channelId_sum" : 40.0 } ],"car_sum" : 40.0 };

var data2 = {
"_id" : "577327c544bd90be508b46cc","sender_sum" : 20.0
},{
"sender_key" : "5710bcc7e66620fd4bc0914f","sender_sum" : 5.0
}
],"senders_sum" : 25.0
},{
"secondLevel_key" : "55fbeb4744bd9090708b4567","sender_group" : [
{
"sender_key" : "5670f993a2f5dbf12e73b763","sender_sum" : 10.0
}
],"senders_sum" : 10.0
}
],"channelId_sum" : 35.0
},{
"channelId_key" : "1","sender_sum" : 20.0
}
],"senders_sum" : 20.0
}
],"channelId_sum" : 20.0
}
],"car_sum" : 55.0
};

合并之后的结果如下:

rush:js;"> { "_id": "577327c544bd90be508b46cc","channelId_info": [ { "channelId_key": "0","secondLevel_group": [ { "secondLevel_key": "568cc36c44bd90625a045c60","sender_group": [ { "sender_key": "577327c544bd90be508b46cd","sender_sum": 60 },{ "sender_key": "5710bcc7e66620fd4bc0914f","sender_sum": 5 } ],"senders_sum": 65 },{ "secondLevel_key": "55fbeb4744bd9090708b4567","sender_group": [ { "sender_key": "5670f993a2f5dbf12e73b763","sender_sum": 10 } ],"senders_sum": 10 } ],"channelId_sum": 75 },{ "channelId_key": "1","sender_sum": 20 } ],"senders_sum": 20 } ],"channelId_sum": 20 } ],"car_sum": 95 }

总结

以上就是这篇文章的全部内容了,文中提到的上述代码在日常工作中很有用,值得大家收藏!希望本文的内容对大家的学习或者工作能带来一定的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“基于nodejs...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建”,内容详细,步骤清晰,细节处理妥当,希望这篇“nodejs怎么实现目录不存在自动创建”文章能帮助大...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内容详细,步骤清晰,细节处理妥当,希望这篇“nodejs如何实现定时删除文件”文章能帮助大家解决疑惑...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
本篇内容主要讲解“怎么安装Node.js的旧版本”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎...
这篇“node中的Express框架怎么安装使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家...
这篇文章主要介绍“nodejs如何实现搜索引擎”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“nodejs如何实现搜索引擎...
这篇文章主要介绍“nodejs中间层如何设置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“nodejs中间层如何设置”文...
这篇文章主要介绍“nodejs多线程怎么实现”,在日常操作中,相信很多人在nodejs多线程怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法...
这篇文章主要讲解了“nodejs怎么分布式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nodejs怎么分布式”...
本篇内容介绍了“nodejs字符串怎么转换为数组”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情...
这篇文章主要介绍了nodejs如何运行在php服务器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇nodejs如何运行在php服务器文章都...
本篇内容主要讲解“nodejs单线程如何处理事件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“nodejs单线程如何...
这篇文章主要介绍“nodejs怎么安装ws模块”,在日常操作中,相信很多人在nodejs怎么安装ws模块问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法...
本篇内容介绍了“怎么打包nodejs代码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!
本文小编为大家详细介绍“nodejs接收到的汉字乱码怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“nodejs接收到的汉字乱码怎么解决”文章能帮助大家解...
这篇“nodejs怎么同步删除文件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇...
今天小编给大家分享一下nodejs怎么设置淘宝镜像的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希