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

How to return an array of properties that match pattern using lodash

本文关键字:属性 数组 模式匹配 何使用 lodash 返回      更新时间:2023-10-19

是否有任何方法可以使用lodash函数返回与模式匹配的属性数组?

_.magicMethod({a:'hi', b:13, c:undefined, d:null, e:null}, null)
return => `['d','e']`

我查看了文档,但没有发现任何内容:/非常感谢。

它可能没有单一的函数版本;但你可以做到:

function magicMethod(obj, value) {
    return _.keys(_.pick(obj, function(propertyValue) {
         return propertyValue === value;
    }));
}

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