如何使用lodash intersectionWith在具有差异结构的两个数组中比较和查找具有相同值的相同属性?

时间:2016-06-02 14:05:54

标签: javascript arrays sorting lodash intersection

例如,我有两组数组

arr1 = [
  {
    myName: 'Adam',
    mySkill: 'CSS',
  },
  {
    myName: 'Mutalib',
    mySkill: 'JavaScript',
  },
];

arr2 = [
  {
    myName: 'Adam',
    myWeight: '112',
  },
  {
    myName: 'Habib',
    myWeight: '221',
  },
];

我尝试比较它们,并选择与lodash的myName函数相同的intersectionWith值。但是一直无法做到

newList.push(intersectionWith(arr1.arr2,isEqual])); 

*在这种情况下,intersectionWith(arr1.arr2,isEqual])的结果应为Adam

有关我应该如何进一步处理的任何建议,解决方案和想法?

此外,之前我在另一个中使用了两个forEach来实现这一目标...除非这是我对这种排序的最好方法吗?

4 个答案:

答案 0 :(得分:3)

您应该使用intersectionBy()来获取交叉点属性。

var result = _.intersectionBy(arr1, arr2, 'myName');

var arr1 = [{
  myName: 'Adam',
  mySkill: 'CSS',
}, {
  myName: 'Mutalib',
  mySkill: 'JavaScript',
}];

var arr2 = [{
  myName: 'Adam',
  myWeight: '112',
}, {
  myName: 'Habib',
  myWeight: '221',
}];

var result = _.intersectionBy(arr1, arr2, 'myName');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

如果您只想使用myName值提取交集,那么您可以使用intersectionBy()map()进行懒惰评估。

var result = _(arr1).intersectionBy(arr2, 'myName').map('myName').value();

var arr1 = [{
  myName: 'Adam',
  mySkill: 'CSS',
}, {
  myName: 'Mutalib',
  mySkill: 'JavaScript',
}];

var arr2 = [{
  myName: 'Adam',
  myWeight: '112',
}, {
  myName: 'Habib',
  myWeight: '221',
}];

var result = _(arr1).intersectionBy(arr2, 'myName').map('myName').value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>

答案 1 :(得分:1)

我们无需使用 lodash

const intersectionBy = (a, b, fn) => {
  const s = new Set(b.map(fn));
  return a.filter(x => s.has(fn(x)));
};

示例:

intersectionBy(arr1, arr2, x => x.myName);

答案 2 :(得分:0)

我可以建议您使用这个能够解决问题的代码:

var jawn = arr1.map(v => v.myName).concat( arr2.map(v => v.myName)).sort().filter(function(item, pos, self) {
   return self.indexOf(item) != pos;
});

console.log(jawn);

答案 3 :(得分:0)

我想为此开发一个通用的可重用解决方案。让我们定义具有至少一个与相似对象具有相同值的公共属性的对象。然后...

&#13;
&#13;
Object.prototype.isSimilar = function(o){ // return true if objects have at least one property with same value in common
  var ok = Object.keys(this);
  return typeof o === "object" ? ok.some(k => this[k] === o[k]) : false;
};
Array.prototype.containsSimilar = function(o){ // return true if it includes a similar object
  return this.some(e => typeof o === "object" ? o.isSimilar(e) : o === e);
};
Array.prototype.getSimilarObjects = function(...a) { // return similar objects in an array
  return [this,...a].reduce((p,c) => p.filter(e => c.containsSimilar(e)));
};

var arr1 = [
  {
    myName: 'Adam',
    mySkill: 'CSS',
  },
  {
    myName: 'Mutalib',
    mySkill: 'JavaScript',
  },
],

    arr2 = [
  {
    myName: 'Adam',
    myWeight: '112',
  },
  {
    myName: 'Habib',
    myWeight: '221',
  },
],
 result = arr1.getSimilarObjects(arr2);
console.log(JSON.stringify(result));
&#13;
&#13;
&#13;