js-nested-pick
v1.1.3
Published
Creates an object composed of the picked object properties from a nested object using a dot path, select specific fields based conditions with filter arrays with MongoDB queries
Downloads
30
Maintainers
Readme
js-nested-pick
Creates an object composed of the picked object properties from a nested object using a dot path, select specific fields based conditions with filter arrays with MongoDB queries
Can be used in js/ts projects in node/browser.
Support select number of fields from nested object with dot and object with query:
How to use:
const pick = require('js-nested-pick');
const data = {
name: 'Dan',
age: 30,
}
const values = pick(data, ['name']) // {name: 'Dan'}
Simple select
['name', 'age']
Select name and age from objectDeep select with dot
['user.firstname']
Select user.name from inner objectOmmit fileds
['-password']
Select all fields except the passwordDeep ommit fileds with dot
['-user.address']
Select all fields except the user addressDeep select/ ommit using object with path
['*', {path: 'user', select: ['-paasword']}]
Select all fields, but in the user object, remove the password fieldSelect field in root object base of query on the data - using sift (read sift docs to see the options)
[{when: {isPublic: true}, then: ['*'], otherwise: ['title']}]
When the data.isPublic equal to true then in the data.posts select all fields, otherwise select only the title (post can be object or array)Select field in inner object or array base of query on the data - using sift (read sift docs to see the options)
[{path: 'post', when: {isPublic: true}, then: ['*'], otherwise: ['title']}]
When the data.isPublic equal to true then in the data.posts select all fields, otherwise select only the title (post can be object or array)Select field in inner object or array base of query on the item data - using sift (read sift docs to see the options)
[{path: 'posts', where: {isPublic: true}, then: ['*'], otherwise: ['title']}]
When the post.isPublic equal to true then select all post fields, otherwise select only the title (post can be object or array)
In-browser Playgrounds
Dependencies:
Test with jest