简体   繁体   中英

intersection of two array of objects basis on the value

I have two arrays like this:

  1. [{ id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" }]

  2. [ { id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 } ]

I want to merge them basis on the refNo. The resulting array would look like:

[
  {
    id: 456,
    refNo: "q1w2e3",
    transactionName: "poke",
    rating: 5
  },
  {
    id: 967,
    refNo: "w3e4r5",
    transactionName: "mon",
    rating: 3.5
  }
]

It will be a one to one mapping. No duplicate refNo will be present in both the arrays.

You can go with something like:

var arrayA = [{ id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" }];
var arrayB = [ { id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 } ];

var result = arrayA.map(function(itemA){
    var itemB = _.find(arrayB, function(x){
        return itemA.refNo === x.refNo;
    });

    return _.merge(itemA, itemB);
});

** Assumed that if same properties exist in both items - the item from second array will win in merge. Merge is from lodash. lodash#merge

Do it with Object.assign , Array#map and Array#find methods

 var arr1 = [{ id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" }]; arr2 = [{ id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 }]; var arr3 = arr2 // iterate over `arr2` and generate array // based on the it's element .map(function(v) { // create a new object and assign properties of both // array object with same `refNo` property return Object.assign({}, // find the object with same `refNo` property arr1.find(function(v1) { // set the condition for find return v1.refNo === v.refNo; // set `v` for extending object property }), v) }) console.log(arr3); 

This might help. Added some comments to explain the steps.

 var arr1 = [{ id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" }]; var arr2 = [ { id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 } ]; var resultArray = []; for (var i = 0, len = arr2.length; i < len; i++) { // find in other array by 'refNo' var findByRef = arr1.filter(function(v) { return v.refNo === arr2[i].refNo; // Filter out the appropriate one }); // merge result var merged = merge_options(findByRef[0], arr2[i]); // add merged to result array resultArray.push(merged); } // dump result array console.log(resultArray); // merge all attrs of two objects function merge_options(obj1,obj2){ var obj3 = {}; for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } return obj3; } 

This might help you.

 var array1 = [{ id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" }]; var array2 = [ { id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 } ]; for(var i = 0 ; i < array1.length ; i++) { var tempRef = array1[i]['refNo']; for(var j = 0 ; j < array2.length ; j ++) { if(array2[j]['refNo'] === tempRef) { array1[i]['rating'] = array2[j]['rating']; } } } console.log(JSON.stringify(array1)); 

To reduce the time needed to loop over all of the items from O(n^2) to O(n), you can use an object whose keys are refNo s.

Something like this:

var uniqueRefNos = {};

var array1 = [ { id: 123, refNo: "q1w2e3", transactionName: "poke" }, { id: 143, refNo: "w3e4r5", transactionName: "mon" } ];
var array2 = [ { id: 456, refNo: "q1w2e3", rating: 5 }, { id: 967, refNo: "w3e4r5", rating: 3.5 } ];

for(var i = 0 ; i < array1.length ; i++){
    //add all objects in array1 to uniqueRefNos
    uniqueRefNos[array1[i].refNo] = array1[i];
}

for(var i = 0 ; i < array2.length ; i++){
    //check if key already exists
    var key = array2[i].refNo;
    if (uniqueRefNos.hasOwnProperty(key)) {
      //if it does add the remaining attributes replacing the previous values from array1
      for (var key2 in array2[i]) {
        if(array2[i].hasOwnProperty(key2)) {
          uniqueRefNos[key][key2] = array2[i][key2];
        }
      }
    } else {
        //if key doesn't exist add the whole object
        uniqueRefNos[array2[i].refNo] = array2[i];
    }
}

This will put all your inner objects inside one object which you can loop over and dump into another array using:

var finalArray = [];
for (var key in uniqueRefNos) {
  if (uniqueRefNos.hasOwnProperty(key)) {
    finalArray.push(uniqueRefNos[key]);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM