รวมค่าภายในอาร์เรย์หลายมิติสองค่าเข้าด้วยกัน

ฉันมีวัตถุสองชิ้นต่อไปนี้:

var object1  = {
  "[email protected]": [
    {
      "status": "Unread",
      "message": "Some message 1",
      "senderId": "0063f297-0da6-4c23-9a53-0480c273d177",
      "id": "088b71bc-5b64-4f09-bba4-cf88b3cc598e",
      "time": "2016-05-13T10:30:11.495Z"
    },
    {
      "status": "Unread",
      "message": "Some other message 2",
      "senderId": "0063f297-0da6-4c23-9a53-0480c273d177",
      "id": "39b2850b-d8ac-4d23-9fa6-e7056fb6c030",
      "time": "2016-05-13T10:30:03.310Z"
    }
  ]
};

var object2 = {
  "[email protected]": [
    {
      "message": "Some message 1",
      "id": "088b71bc-5b64-4f09-bba4-cf88b3cc598e",
      "time": "2016-05-13T10:30:11.495Z"
    },
    {
      "message": "Some other message 2",
      "id": "39b2850b-d8ac-4d23-9fa6-e7056fb6c030",
      "time": "2016-05-13T10:30:03.310Z"
    }
  ],
  "[email protected]": [
    {
      "message": "Some message 1",
      "id": "088b71bc-5b64-4f09-bba4-cf88b3cc598e",
      "time": "2016-05-13T10:30:11.495Z"
    },
    {
      "message": "Some other message 1",
      "id": "39b2850b-d8ac-4d23-9fa6-e7056fb6c030",
      "time": "2016-05-13T10:30:03.310Z"
    }
  ]
};

ฉันต้องรวมพวกมันให้เป็นวัตถุเดียวที่มีลักษณะดังนี้:

var object3 = {
  "[email protected]": [
    {
      "status": "Unread",
      "message": "Some message 1",
      "senderId": "0063f297-0da6-4c23-9a53-0480c273d177",
      "id": "088b71bc-5b64-4f09-bba4-cf88b3cc598e",
      "time": "2016-05-13T10:30:11.495Z"
    },
    {
      "status": "Unread",
      "message": "Some other message 2",
      "senderId": "0063f297-0da6-4c23-9a53-0480c273d177",
      "id": "39b2850b-d8ac-4d23-9fa6-e7056fb6c030",
      "time": "2016-05-13T10:30:03.310Z"
    },
    {
      "message": "Some message 1",
      "id": "088b71bc-5b64-4f09-bba4-cf88b3cc598e",
      "time": "2016-05-13T10:30:11.495Z"
    },
    {
      "message": "Some other message 2",
      "id": "39b2850b-d8ac-4d23-9fa6-e7056fb6c030",
      "time": "2016-05-13T10:30:03.310Z"
    }
  ]
}

โดยที่อีเมลที่ระบุกลุ่มข้อความเหมือนกัน


person dadidaochieng    schedule 27.05.2016    source แหล่งที่มา
comment
[email protected] ไม่อยู่ในรายการผลลัพธ์เนื่องจากไม่มีอยู่ใน ทั้ง array1 และ array2?   -  person iulian    schedule 27.05.2016
comment
ใช่ รวมเฉพาะโหนด [email protected] เท่านั้น   -  person dadidaochieng    schedule 27.05.2016


คำตอบ (3)


ขั้นแรก รับกุญแจจากแต่ละวัตถุ จากนั้น สำหรับอีเมลแต่ละฉบับที่มีอยู่ในทั้งสองออบเจ็กต์ ให้เชื่อมต่อรายการเข้าด้วยกัน

var keys1List = Object.getOwnPropertyNames(array1),
    keys2List = Object.getOwnPropertyNames(array2),
    result = {};

keys1List.forEach(function(email) {
    if (keys2List.indexOf(email) > -1) {
        result[email] = array1[email].concat(array2[email]);
    }
});

โปรดทราบว่าตัวแปร array1, array2 และ array3 ของคุณเป็นวัตถุจริงๆ ไม่ใช่อาร์เรย์ ฉันขอแนะนำให้คุณเปลี่ยนชื่อเพื่อไม่ให้คนอื่นสับสน (หรือแม้แต่คุณในภายหลัง) ที่จะอ่านโค้ดนี้

person iulian    schedule 27.05.2016
comment
ขอบคุณ ทำงานเหมือนเวทมนตร์ ใช่แล้ว กำลังเปลี่ยนชื่อเป็นวัตถุ - person dadidaochieng; 27.05.2016

รวมกันตามที่คุณต้องการ:

Object.combineArrays = function(obj1, obj2) {
   var sizeA = 0, key, keyB;
   for (key in obj2) {
      if (obj1.hasOwnProperty(key) && obj2.hasOwnProperty(key)) {
        for (keyB in obj2[key]) {
           obj1['[email protected]'].push( obj2[key][keyB] );
        }
        sizeA++;
      }
   }
   return obj1;
};
var newObjects = Object.combineArrays(obj1, obj2);
console.log(newObjects);
person v1000    schedule 27.05.2016

ใช้ขีดล่าง

_.mapObject(array1,function(val,key){
    return _.union(val,array2[key]);
})
person gu mingfeng    schedule 27.05.2016
comment
ไม่มีแท็กขีดล่างในคำถาม ดังนั้นเราจึงควรให้คำตอบ vanilla-js :) - person Shilly; 27.05.2016