Skip to content
/ pick Public

Return partial copy of object containing specified subset of keys.

Notifications You must be signed in to change notification settings

micro-js/pick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pick

Build status Git tag NPM version Code style

Return partial copy of object containing specified subset of keys.

Installation

$ npm install @f/pick

Usage

var pick = require('@f/pick')

var source = {a: 1, b: 2, c: 3, d: function () {}}
var src1 = {a: 2}
var src2 = {a: 1, b: 2}

pick(['a', 'c'], source)     // => {a: 1, c: 3}
pick('a', source)            // => {a: 1}
pick(isFunction, source)     // => {d: function () {}}
pick(['a', 'b'], src1, src2) // => {a: 2, b: 2}

API

pick(keys, ...objs)

  • keys - Key(s) to pick, or predicate function that receives (val, key).
  • ...objs - Source objects to pick from, the object in the leftmost position has the highest priority. Multiple objects do not work with predicate functions.

Returns: partial copy of obj

Progressive picking

This pick implementation has a somewhat unique feature - it can pick progressively from objects until the key is found. If you pass multiple objects it will, starting from the left hand side, use the first value in the list that is not undefined. E.g.

var src1 = {a: 2}
var src2 = {a: 1, b: 2}

pick(['a', 'b'], src1, src2) // => {a: 2, b: 2}

License

MIT

About

Return partial copy of object containing specified subset of keys.

Resources

Stars

Watchers

Forks

Packages

No packages published