Remove items from one array of objects from another array of objects with _.differenceBy

余生颓废 提交于 2021-02-04 08:18:05

问题


I have two arrays of objects:

var defendantList = [
  {
    label: "Joe BLow"
    value: "Joe Blow"
  },
  {
    label: "Sam Snead"
    value: "Sam Snead"
  },
  {
    label: "John Smith"
    value: "John Smith"
  },
];

var dismissedDefendants = [
  {
    date: 'someDateString',
    value: "Joe Blow"
  },
  {
    date: "someOtherDateString",
    value: "Sam Snead"
  }
];

I need to create an array that has values from defendantList that are not contained in dismissedDefendants. How can I do that simply, either with lodash or a standard JS array function? I'm looking at lodash's _.differenceBy, since it has an iteratee, but I can't quite figure out how.

UPDATE: the desired end result in this example is just an array with the non-matching object:

  var newArray = [
      {
        label: "John Smith"
        value: "John Smith"
      },
    ];

Thanks.


回答1:


Using _.differenceBy():

_.differenceBy(defendantList, dismissedDefendants, 'value');

var defendantList = [
  {
    label: "Joe BLow",
    value: "Joe Blow"
  },
  {
    label: "Sam Snead",
    value: "Sam Snead"
  },
  {
    label: "John Smith",
    value: "John Smith"
  },
];

var dismissedDefendants = [
  {
    date: 'someDateString',
    value: "Joe Blow"
  },
  {
    date: "someOtherDateString",
    value: "Sam Snead"
  }
];

var result = _.differenceBy(defendantList, dismissedDefendants, 'value');

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

And an ES6 solution based on Array.prototype.filter() and Set:

defendantList.filter(function({ value }) { 
  return !this.has(value); // keep if value is not in the Set
}, new Set(dismissedDefendants.map(({ value }) => value))); //create a Set of unique values in dismissedDefendants and assign it to this

var defendantList = [
  {
    label: "Joe BLow",
    value: "Joe Blow"
  },
  {
    label: "Sam Snead",
    value: "Sam Snead"
  },
  {
    label: "John Smith",
    value: "John Smith"
  },
];

var dismissedDefendants = [
  {
    date: 'someDateString',
    value: "Joe Blow"
  },
  {
    date: "someOtherDateString",
    value: "Sam Snead"
  }
];

var result = defendantList.filter(function({ value }) { 
  return !this.has(value); 
}, new Set(dismissedDefendants.map(({ value }) => value)));

console.log(result);



回答2:


Here's a simple solution using plain Javascript. Hope it helps!

var defendantList = [
  {
    label: "Joe BLow",
    value: "Joe Blow"
  },
  {
    label: "Sam Snead",
    value: "Sam Snead"
  },
  {
    label: "John Smith",
    value: "John Smith"
  },
];

var dismissedDefendants = [
  {
    date: 'someDateString',
    value: "Joe Blow"
  },
  {
    date: "someOtherDateString",
    value: "Sam Snead"
  }
];

for(var i in defendantList){
    for(var j in dismissedDefendants){
      if(defendantList[i].value === dismissedDefendants[j].value){
        var index = defendantList.indexOf(defendantList[i]);
        if (index > -1) {
        defendantList.splice(index, 1);
        }
      }
    }
}
console.log(defendantList);


来源:https://stackoverflow.com/questions/40837662/remove-items-from-one-array-of-objects-from-another-array-of-objects-with-diff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!