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

前端之家收集整理的这篇文章主要介绍了Node.js中如何合并两个复杂对象详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

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

Node.js合并两个复杂对象

例如我有以下两个object:

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
}
]
};

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

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

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;
}

// 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] = 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;
}

例如有以下两个对象:

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
};

合并之后的结果如下:

总结

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

猜你在找的Node.js相关文章