碎片时间学编程「359]:拒绝不匹配的值

根据谓词函数过滤数组的值,仅返回谓词函数返回 false 的值。 将 Array.prototype.filter() 与谓词函数 pred 结合使用,仅返回返回 false 的值。
const reject = (pred, array) => array.filter((...args) => !pred(...args));
示例:
reject(x => x % 2 === 0, [1, 2, 3, 4, 5]); // [1, 3, 5]reject(word => word.length > 4, ['Apple', 'Pear', 'Kiwi', 'Banana']);// ['Pear', 'Kiwi']
更多内容请访问我的网站:https://www.icoderoad.com