Skip to content

Instantly share code, notes, and snippets.

@danfascia
Created February 15, 2019 10:48
Show Gist options
  • Save danfascia/33543a2bf2e1800dc5b3957d94e30b22 to your computer and use it in GitHub Desktop.
Save danfascia/33543a2bf2e1800dc5b3957d94e30b22 to your computer and use it in GitHub Desktop.
11ty filter to filter items in a collection (array) whose key === value. (source: https://paulrobertlloyd.com/)
/**
* Select objects in array whose key matches a value
*
* @param {Array} arr Array to test
* @param {String} key Key to inspect
* @param {String} value Value key needs to match
* @return {String} Filtered array
*
*/
module.exports = function (arr, key, value) {
return arr.filter(item => {
const keys = key.split('.');
const reduce = keys.reduce((object, key) => {
return object[key];
}, item);
return (reduce === value ? item : false);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment