https://github.com/sindresorhus/filter-obj
Filter object keys and values into a new object
https://github.com/sindresorhus/filter-obj
Last synced: about 1 year ago
JSON representation
Filter object keys and values into a new object
- Host: GitHub
- URL: https://github.com/sindresorhus/filter-obj
- Owner: sindresorhus
- License: mit
- Created: 2015-08-10T05:42:04.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-05-27T10:48:45.000Z (almost 2 years ago)
- Last Synced: 2025-04-03T12:11:45.445Z (about 1 year ago)
- Language: JavaScript
- Size: 37.1 KB
- Stars: 101
- Watchers: 9
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-micro-npm-packages - filter-obj - Filter object keys and values into a new object. (Modules / Object)
- awesome-micro-npm-packages - filter-obj - Filter object keys and values into a new object. (Modules / Object)
- awesome-micro-npm-packages-zh - filter-obj - 将对象键值和值过滤到新对象中. (模块 / 对象)
- fucking-awesome-micro-npm-packages - filter-obj - Filter object keys and values into a new object. (Modules / Object)
README
# filter-obj
> Filter object keys and values into a new object
## Install
```sh
npm install filter-obj
```
## Usage
```js
import {includeKeys, excludeKeys} from 'filter-obj';
const object = {
foo: true,
bar: false
};
const newObject = includeKeys(object, (key, value) => value === true);
//=> {foo: true}
const newObject2 = includeKeys(object, ['bar']);
//=> {bar: false}
const newObject = excludeKeys(object, (key, value) => value === true);
//=> {bar: false}
const newObject3 = excludeKeys(object, ['bar']);
//=> {foo: true}
```
## API
### includeKeys(source, filter)
### includeKeys(source, keys)
### excludeKeys(source, filter)
### excludeKeys(source, keys)
#### source
Type: `object`
The source object to filter properties from.
#### filter
Type: `(sourceKey: string | symbol, sourceValue: unknown, source: object) => boolean`
A predicate function that determines whether a property should be filtered.
#### keys
Type: `Array | Set`
An array or [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) of property keys to be filtered.
## Related
- [map-obj](https://github.com/sindresorhus/map-obj) - Map object keys and values into a new object