基于值的两个对象数组的交集

时间:2016-08-04 05:12:34

标签: javascript arrays underscore.js

我有两个这样的数组:

  1. [{ id:123, refNo:“q1w2e3”, transactionName:“戳” }, { id:143, refNo:“w3e4r5”, transactionName:“mon” }]

  2. [   {     id:456,     refNo:“q1w2e3”,     评分:5   },   {     id:967,     refNo:“w3e4r5”,     评分:3.5   } ]

  3. 我想在refNo上合并它们。结果数组看起来像:

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

    这将是一对一的映射。两个阵列中都不会出现重复的refNo。

5 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

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

**假设如果两个项目中都存在相同的属性 - 第二个数组中的项目将在合并中获胜。合并来自lodash。 lodash#merge

答案 1 :(得分:2)

使用 Object.assign Array#map Array#find 方法

进行此操作

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

答案 2 :(得分:1)

这可能会有所帮助。添加了一些注释来解释这些步骤。



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;
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这可能会对你有帮助。

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

答案 4 :(得分:0)

为了减少将所有项目从O(n ^ 2)循环到O(n)所需的时间,您可以使用键为refNo s的对象。

这样的事情:

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

这会将所有内部对象放在一个对象中,您可以使用以下方法将其循环并转储到另一个数组中:

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