Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Last active April 19, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mxriverlynn/d07f87cb2037578c6784 to your computer and use it in GitHub Desktop.
Save mxriverlynn/d07f87cb2037578c6784 to your computer and use it in GitHub Desktop.
sets and array difference in ES6
// initial set created from array
var mySet = new Set([1, 2, 3, 2, 4, 1, 3, 5]);
// add more items
mySet.add(1);
mySet.add(3);
// see what it holds
console.log(mySet); // => Set { 1, 2, 3, 4, 5 }
var completeList = [/* ... filled in elsewhere */]
var invalidList = [/* ... also filled in elsewhere */]
// filter the items from the invalid list, out of the complete list
var validList = completedList.filter((item) => {
return !invalidList.has(item);
})
// get a Set of the distinct, valid items
var validItems = new Set(validList);
@BigGillyStyle
Copy link

Small typo in 2.js...

var validList = completedList.filter((item) should be

var validList = completeList.filter((item)

@dfloresgonz
Copy link

dfloresgonz commented Apr 19, 2018

Hope this modified version helps :)

var completedList = [1,2,3,4];
var invalidList = new Set([3,4,5,6]);
// filter the items from the invalid list, out of the complete list
var validList = completedList.filter((item) => {
  return !invalidList.has(item);
})
console.log(validList);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment