1.0.1 • Published 5 months ago

pick-exist-fields v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

pick-exist-fields

Pick exist fields from an object

npm npm

Utility function to select specified field's value is not null and undefined from an plain object

If the object is not plain object, then return the object directly.

Usage

npm install pick-exist-fields --save
import pickExistFields from "pick-exist-fields";

const originalObj = { 
  name: "npm", 
  age: 18, 
  career: "engineer", 
  address: null
};

// { age: 18 }
pickExistFields(originalObj, "age"); 

// { age: 18, career: 'engineer' }
pickExistFields(originalObj, ["age", "career"]); 

// { name: 'npm', age: 18, career: 'engineer' } 
pickExistFields(originalObj); 

API

pickExistFields(obj: object, fields: string | string[]) : object

return a shallow copy which is composed of the picked fields, if the field value is not null and undefined.

if fields is not specified, then pick all non-null/non-undefined fields.

LICENSE

MIT

1.0.1

5 months ago

1.0.0

5 months ago