使用lodash的特定对象属性值

fas*_*ava 1 javascript object lodash

我有一个对象:

{
    "x" : "a",
    "y" : "b",    
    "z" : "c",    
}
Run Code Online (Sandbox Code Playgroud)

我有一个数组:['x', 'y'].如何从上面的对象获得结果:

{
    "x" : "a",
    "y" : "b"
}
Run Code Online (Sandbox Code Playgroud)

Ale*_* T. 7

您可以使用 _.pick

var data = {
    "x" : "a",
    "y" : "b",    
    "z" : "c",    
};

console.log(_.pick(data, ['x', 'y']));
console.log(_.pick(data, 'x', 'y'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)