使用lodash的特定对象属性值

时间:2015-04-07 16:16:37

标签: javascript object lodash

我有一个对象:

{
    "x" : "a",
    "y" : "b",    
    "z" : "c",    
}

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

{
    "x" : "a",
    "y" : "b"
}

1 个答案:

答案 0 :(得分:7)

您可以使用_.pick

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

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