-
-
Save mxriverlynn/d07f87cb2037578c6784 to your computer and use it in GitHub Desktop.
sets and array difference in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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
Small typo in 2.js...
var validList = completedList.filter((item)
should bevar validList = completeList.filter((item)