Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active August 4, 2022 06:29
Show Gist options
  • Save sethdavis512/a387e8e94a967cb1962a1ca04f774c65 to your computer and use it in GitHub Desktop.
Save sethdavis512/a387e8e94a967cb1962a1ca04f774c65 to your computer and use it in GitHub Desktop.
Filter array based on object key value pairs.
const valuesMatch = (sourceObj, matchObj) => {
let matches = true;
Object.keys(matchObj).forEach(matchKey => {
if (sourceObj[matchKey] !== matchObj[matchKey]) {
matches = false;
}
});
return matches;
};
const filterData = (data, paramsObj) => {
return data.reduce((acc, cur) => {
if (valuesMatch(cur, paramsObj)) {
acc.push(cur);
}
return acc;
}, []);
};
const results = filterData(screenshotData, {
region: "US"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment