1.0.2 • Published 5 years ago

get-obj v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

get-obj

Filter and replace key/value object to new object

Installation

npm install get-obj

Usage

const getObj = require('get-obj');

const exampleObj = {
    id: 1,
    title: 'phatpro',
    details: {
        description: 1,
        createAt: 1,
        updateAt: 1
    }
}

// Filter
var newObj = getObj(exampleObj, (key, value) => key.indexOf('title') === 0)
// { title: 'phatpro' }

// Filter and replace key
var newObj = getObj(exampleObj, (key, value) =>
    key.indexOf('title') === 0 && 'hiThere'
)
// { hiThere: 'phatpro' }

// Filter and replace key/value
var newObj = getObj(exampleObj, (key, value) =>
    key.indexOf('title') === 0 && ['hiThere', 'hi']
)
// { hiThere: 'hi' }

Params

  • obj: origin object
  • predicate: predicate function

License

MIT © phatpro