https://github.com/nicklason/node-select-properties
Specify which properties to include or exclude in an object or an array of objects
https://github.com/nicklason/node-select-properties
exclude fields include javascript node properties select
Last synced: 11 months ago
JSON representation
Specify which properties to include or exclude in an object or an array of objects
- Host: GitHub
- URL: https://github.com/nicklason/node-select-properties
- Owner: Nicklason
- License: mit
- Created: 2018-08-12T12:30:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-01-03T18:49:07.000Z (about 7 years ago)
- Last Synced: 2024-04-25T14:22:31.738Z (almost 2 years ago)
- Topics: exclude, fields, include, javascript, node, properties, select
- Language: JavaScript
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-select-properties
Specify which properties to include or exclude in an object or an array of objects
[](https://www.npmjs.com/package/select-properties)
[](https://travis-ci.org/Nicklason/node-select-properties)
[](https://coveralls.io/github/Nicklason/node-select-properties)
[](https://nodei.co/npm/select-properties/)
# Introduction
I made this module to help me use [cachegoose](https://www.npmjs.com/package/cachegoose). I wanted to be able to cache all properties from queries, but only select specific fields. This is because I have two applications using the same [mongodb](https://www.mongodb.com/) and [redis](https://redis.io/) database, but they don't need the same data.
This module works the same way as mongoose's [select](http://mongoosejs.com/docs/queries.html) function, only difference is that you give it the result, and then give it the fields you want selected.
*This is the reason to why I've made this module, but you can use it to solve your own problems!*
It can be used
# Examples
```js
const selectProperties = require('select-properties');
const target = { foo: '', bar: '' };
// Object to select the fields of
const select = { foo: 0 };
// Exclude the foo property
const result = selectProperties(target, select);
// The result will be { bar: '' }
```
Select nested properties using dot notation.
```js
const target = {
some: {
nested: {
value: ''
},
other: {
nested: {
value: ''
}
}
}
};
const select = '-some.nested';
const result = selectProperties(target, select);
// The result will be { some: { other: { nested: { value: '' } } } }
```
---
**NOTE**
Both nested paths, and properties, that match the selected field will be targeted.
See example below.
---
```js
const target = {
some: {
nested: {
value: ''
},
other: {
nested: {
value: ''
}
}
},
'some.nested': {
value: ''
}
};
const select = '-some.nested';
const result = selectProperties(target, select);
// The result will be { some: { other: { nested: { value: '' } } } }
```
Select the properties of multiple objects.
```js
const target = [{ foo: '', bar: '' }, { foo: '', bar: '' }];
// Array of objects to select the fields of
const select = { foo: 0 };
const result = selectProperties(target, select);
// The result will be [{ bar: '' }, { bar: '' }]
```
Select using a string.
```js
const select = '-foo';
// Exclude foo
```
Select specific properties.
```js
const target = [{ foo: '', bar: '' }, { foo: '', bar: '' }];
// Array of objects to select the fields of
const select = { foo: 1 };
const result = selectProperties(target, select);
// The result will be [{ foo: '' }, { foo: '' }]
```