UNPKG

es-next-tools

Version:

A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.

24 lines (23 loc) 729 B
/** * Filters an object based on a predicate function. * @param {T} object - The object to filter. * @param {Function} predicate - The function to test each entry of the object. * @returns A new object with entries that passed the test. * @example * const obj = { a: 1, b: 2, c: 3, d: 4 }; * const filtered = filter(obj, ([key, value]) => value % 2 === 0); * // { b: 2, d: 4 } */ export function filter(object, predicate) { const result = {}; for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { const value = object[key]; if (predicate([key, value])) { result[key] = value; } } } ; return result; }