如何使用 lodash 返回匹配模式的属性数组

use*_*903 3 javascript arrays properties underscore.js lodash

有没有办法使用该lodash函数返回与模式匹配的属性数组?

_.magicMethod({a:'hi', b:13, c:undefined, d:null, e:null}, null)

return => `['d','e']`
Run Code Online (Sandbox Code Playgroud)

我检查了文档,但什么也没找到:/谢谢。

Ret*_*sam 5

可能没有它的单一功能版本;但你可以这样做:

function magicMethod(obj, value) {
    return _.keys(_.pick(obj, function(propertyValue) {
         return propertyValue === value;
    }));
}
Run Code Online (Sandbox Code Playgroud)

_.pick创建一个仅具有与指定值匹配的属性的对象,然后_.keys提取该对象的键。