1

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
  • @HenryDev No, since I know in this case label and value are the same. Commented Nov 28, 2016 at 5:30

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.