Skip to content

raisch/lodash-sift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lodash-sift

A lodash "mixin" that returns a new object constructed using one or more supplied paths into an existing object.

npm version Build Status Dependencies Status DevDependencies Status

Description

Given a data object and a set of one or more dot-separated paths, _.sift() will construct and return a new object composed of only the values found at the selected paths.

Example

var _ = require('lodash-sift'); // returns lodash with sift mixin

var data = { // object from which to extract paths
      a: {
        b: {
          c: 1,
          d: 2
        },
        e: {
          f: 3,
          g: 4
        }
      },
      h: [5, 6, 7],
      i: [{name: 'foo'}, {name: 'bar'}]
    };
    
var paths = [ // paths to extract from data
      'a.b.d',
      'a.e.g',
      'h[2]',
      'i[1].name'
    ];

_.sift(data, paths)

// => { a: { b: { d: 2 }, e: { g: 4 } }, h: 7, i: { name: 'bar' } }

All paths must be terminal (they must end up at a non-object.)

If a path is non-terminal, it will result in an undefined value in the result, as in:

var data={a:{b:{c:1}}};

_.sift(data,'a.b') // => { a: { b: undefined } }

If a path does not exist, it will be undefined in the result, as in:

var data={ a: { b: { c: 1 } } };

_.sift(data,'a.b.foo') // => { a: { b: { foo: undefined } } }

About

Returns an object consisting of only the selected paths.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published