Created
January 15, 2019 09:50
-
-
Save hadnazzar/593345f5a667a328168ff514870375df to your computer and use it in GitHub Desktop.
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
/* Here's an example that uses (some) ES6 Javascript semantics to filter an object array by another object array. */ | |
// x = full dataset | |
// y = filter dataset | |
let x = [ | |
{"val": 1, "text": "a"}, | |
{"val": 2, "text": "b"}, | |
{"val": 3, "text": "c"}, | |
{"val": 4, "text": "d"}, | |
{"val": 5, "text": "e"} | |
], | |
y = [ | |
{"val": 1, "text": "a"}, | |
{"val": 4, "text": "d"} | |
]; | |
// Use map to get a simple array of "val" values. Ex: [1,4] | |
let yFilter = y.map(itemY => { return itemY.val; }); | |
// Use filter and "not" includes to filter the full dataset by the filter dataset's val. | |
let filteredX = x.filter(itemX => !yFilter.includes(itemX.val)); | |
// Print the result. | |
console.log(filteredX); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment