Skip to content

Instantly share code, notes, and snippets.

@danfascia
Created February 16, 2019 21:17
Show Gist options
  • Save danfascia/8bce8eb96e2f607be558443d5d0b34fa to your computer and use it in GitHub Desktop.
Save danfascia/8bce8eb96e2f607be558443d5d0b34fa to your computer and use it in GitHub Desktop.
11ty filter to remove items from a filtered array by key:value pair - i.e. {% collection_array... | is-not: 'url', page.url %} (source: https://paulrobertlloyd.com/)
/**
* Remove 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 ? false : item);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment