1.0.1 • Published 7 years ago
lodash-pickall v1.0.1
lodash-pickall
A lodash "mixin" that returns a new object constructed using supplied paths into an existing object. This mixin is inspired from lodash-sift and is more advanced.
Description
Given a data object and a set of paths, _.pickAll()
will
construct and return a new object composed of only the values found at the selected
paths.
Example
var _ = require('lodash-pickAll'); // returns lodash with pickAll mixin
var data = {
a: 1,
b: 2,
c: {
d: 3,
e: 4,
f: {
g: 5,
h: 6
},
i: [{
j: 7
},
{
j: [{
k: 8
},
{
k: 8
},
{
k: 8
},
{
k: 8
},
{
k: 8
}
]
}
]
},
d: {
e: 9
}
};
var paths = ['a',
'b',
{
'c': ['d', 'e', {
'i': ['j', {
'j': ['k']
}]
}],
'd': ['e'],
'e': ['f']
}
]
_.pickAll(data, paths)
// => {a:1,b:2,c:{d:3,e:4,i:[[{j:7},{j:[{k:8},{k:8},{k:8},{k:8},{k:8}]}]]},d:{e:9},e:undefined};
If a path does not exist, it will be undefined in the result, as in:
var data={ a: { b: { c: 1 } } };
_.pickAll(data, [{'a': [{'b':['foo']}]}]) // => { a: { b: { foo: undefined } } }