Created
February 16, 2019 21:17
-
-
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/)
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
/** | |
* 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