如何在Node.js中合并兩個複雜物件

來源:互聯網
上載者:User

標籤:ons   tool   技術   jid   blog   any   jar   有用   test   

 通常情況下,在Node.js中我們可以通過underscore的extend或者lodash的merge來合并兩個對象,但是對於像下面這種複雜的對象,要如何來應對呢?

  例如我有以下兩個object:

var obj1 = {    "name" : "myname",    "status" : 0,    "profile": { "sex":"m", "isactive" : true},    "strarr":["one", "three"],    "objarray": [    {        "id": 1,        "email": "[email protected]",        "isactive":true    },    {        "id": 2,        "email": "[email protected]",        "isactive":false    }    ]};var obj2 = {    "name" : "myname",    "status" : 1,    "newfield": 1,    "profile": { "isactive" : false,  "city": "new York"},    "strarr":["two"],    "objarray": [    {        "id": 1,        "isactive":false    },    {        "id": 2,        "email": "[email protected]"    },    {        "id": 3,        "email": "[email protected]",        "isactive" : true    }    ]};

  希望合并之後的結果輸出成下面這樣:

{ name: ‘myname‘,  status: 1,  profile: { sex: ‘m‘, isactive: false, city: ‘new York‘ },  strarr: [ ‘one‘, ‘three‘, ‘two‘ ],  objarray:   [ { id: 1, email: ‘[email protected]‘, isactive: false },   { id: 2, email: ‘[email protected]‘, isactive: false },   { id: 3, email: ‘[email protected]‘, isactive: true } ],newfield: 1 }

   通過underscore或者lodash現有的方法我們無法實現上述結果,那隻能自己寫代碼來實現了。

function mergeObjs(def, obj) {  if (!obj) {    return def;  } else if (!def) {    return obj;  }  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類型的值自動相加。

function merge(def, obj) {    if (!obj) {        return def;    }    else if (!def) {        return obj;    }    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 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",    "channelId_info" : [    {        "channelId_key" : "0",        "secondLevel_group" : [        {            "secondLevel_key" : "568cc36c44bd90625a045c60",            "sender_group" : [            {                "sender_key" : "577327c544bd90be508b46cd",                "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",        "secondLevel_group" : [        {            "secondLevel_key" : "568cc36c44bd90625a045c60",            "sender_group" : [            {                "sender_key" : "577327c544bd90be508b46cd",                "sender_sum" : 20.0            }            ],            "senders_sum" : 20.0        }        ],        "channelId_sum" : 20.0    }    ],    "car_sum" : 55.0};

  合并之後的結果如下:

{    "_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",            "secondLevel_group": [                {                    "secondLevel_key": "568cc36c44bd90625a045c60",                    "sender_group": [                        {                            "sender_key": "577327c544bd90be508b46cd",                            "sender_sum": 20                        }                    ],                    "senders_sum": 20                }            ],            "channelId_sum": 20        }    ],    "car_sum": 95}

  上述代碼在日常工作中很有用,值得收藏!

如何在Node.js中合并兩個複雜物件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.