Javascript 根据值过滤对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29282273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filtering object properties based on value
提问by user3598395
Is there some elegant way of filtering out falsey properties from this object with lodash/underscore? Similar to how _.compact(array)removes falsey elements from arrays
是否有一些优雅的方法可以使用 lodash/下划线从该对象中过滤掉虚假属性?类似于如何_.compact(array)从数组中删除虚假元素
so from
所以从
{
propA: true,
propB: true,
propC: false,
propD: true,
}
returning
回来
{
propA: true,
propB: true,
propD: true,
}
回答by Retsam
Prior to Lodash 4.0
在 Lodash 4.0 之前
You want _.pick, it takes a function as an argument and returns an object only containing the keys for which that function returns truthy. So you can do:
你想要_.pick,它接受一个函数作为参数并返回一个只包含该函数返回真值的键的对象。所以你可以这样做:
filtered = _.pick(obj, function(value, key) {return value;})
Or even more succinctly:
或者更简洁:
filtered = _.pick(obj, _.identity)
Lodash 4.0
洛达什 4.0
Lodash 4.0 split the _.pickfunction into _.pick, which takes an array of properties, and _.pickBywhich takes a function. So now it'd be
Lodash 4.0 将_.pick函数拆分为_.pick,它接受一个属性数组,_.pickBy后者接受一个函数。所以现在是
filtered = _.pickBy(obj, function(value, key) {return value;})
Or, since _.pickBydefaults to using _.identityas it's second argument, it can just be written as:
或者,由于_.pickBy默认使用_.identity作为它的第二个参数,它可以写成:
filtered = _.pickBy(obj);
回答by canon
Here are two vanilla javascript options:
这里有两个普通的 javascript 选项:
A.:Iterate over the object's keys and deletethose having a falsey value.
A.:迭代对象的键和delete那些具有假值的键。
var obj = {
propA: true,
propB: true,
propC: false,
propD: true,
};
Object.keys(obj).forEach(key => {
if (!obj[key]) delete obj[key];
});
console.log(obj);
See Object.keys()and Array.prototype.forEach()
查看和Object.keys()Array.prototype.forEach()
B.:Iterate over the object's keys and add truthy values to a new object.
B.:迭代对象的键并将真值添加到新对象。
var obj = {
propA: true,
propB: true,
propC: false,
propD: true,
};
var filteredObj = Object.keys(obj).reduce((p, c) => {
if (obj[c]) p[c] = obj[c];
return p;
}, {});
console.log(filteredObj);
回答by Adam Boduch
If you're using lodash, I'd recommend something like this:
如果您使用 lodash,我会推荐这样的东西:
var object = {
propA: true,
propB: true,
propC: false,
propD: true,
};
_.pick(object, _.identity);
// →
// {
// propA: true,
// propB: true,
// propD: true
// }
The pick()function generates a new object that includes properties that the callback returns truthyfor. So we can just use the identity()function as the callback, since it'll just return each property value.
的拾取()函数生成一个新的对象,其包括属性回调返回truthy对。所以我们可以只使用identity()函数作为回调,因为它只会返回每个属性值。
回答by Jonas
Unfortunately I cannot direclty comment on the posts above yet, so I create this extra post.
不幸的是,我还不能直接评论上面的帖子,所以我创建了这个额外的帖子。
Since Lodash v4 the functionality described above has been moved to _.pickBy. With _.identityas default you could also change your code to:
自 Lodash v4 以来,上述功能已移至_.pickBy。随着_.identity默认你也可以改变你的代码:
var filtered = _.pickBy(obj);
See this JSBinfor a working example.
有关工作示例,请参阅此JSBin。
回答by Subrata Sarkar
From lodash 4, we can use pickBy() to get only the value equal to true.
从 lodash 4 开始,我们可以使用 pickBy() 只获取等于 true 的值。
const active = _.keys(_.pickBy(object));

