Filter
Filter unwanted values from an object
๐ฌ Usage
<script>
import {filter} from "@sveu/shared/dicts"
const obj = {created_at: "2022-10-10", updated_at: "2021-10-10", deleted_at: "2020"}
const falsy_obj = {a: 1, b: 2, c: 0, d: null, e: undefined, f: false, g: ""}
filter(obj, (x) => x.includes("2022")) // { updated_at: "2021-10-10" ,deleted_at: "2020" }
filter(falsy_obj) // {a: 1, b: 2}
</script>
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
obj | object to filter | Record<string | number | symbol, any> | yes |
filter | filter function | (value: any) => boolean | no |
โฉ๏ธ Returns
A new object with filtered values.
๐งช Playground
Source Code ๐
Source Code
/**
* filter unwanted values.
*
* @param obj - the object to be filtered
*
* @param filter - the filter function, if the function returns true, the value will be removed.
*
* @returns the filtered object
*/
export function filter<RemovedKeys extends string, T>(
obj: Record<string | number | symbol, any>,
filter: (value: any) => boolean = (x) => !x
): Omit<T, RemovedKeys> {
if (!obj) return {} as T
return Object.keys(obj).reduce((acc, key) => {
if (filter(obj[key])) {
return acc
} else return { ...acc, [key]: obj[key] }
}, {} as T)
}
Last update: 2023-02-14
Authors: Mohamed-Kaizen