基于值过滤对象属性

时间:2022-05-26 22:20:43

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/下划线从这个对象中过滤出falsey属性?类似于_.compact(数组)从数组中移除falsey元素

so from

所以从

{
  propA: true,
  propB: true,
  propC: false,
  propD: true,
}

returning

返回

{
  propA: true,
  propB: true,
  propD: true,
}

4 个解决方案

#1


16  

Prior to 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,它将一个函数作为参数并返回一个对象,该对象只包含该函数返回truthy的键。所以你能做什么:

filtered = _.pick(obj, function(value, key) {return value;})

Or even more succinctly:

甚至更简洁地说:

filtered = _.pick(obj, _.identity)

Lodash 4.0

Lodash 4.0 split the _.pick function into _.pick, which takes an array of properties, and _.pickBy which takes a function. So now it'd be

Lodash 4.0拆分了_。选择函数_。pick,它取一个属性数组,还有_。它具有一个函数。所以现在会

filtered = _.pickBy(obj, function(value, key) {return value;})

Or, since _.pickBy defaults to using _.identity as it's second argument, it can just be written as:

或者,因为_。pickBy默认使用_。同一性作为第二个论点,可以写成:

filtered = _.pickBy(obj);

#2


4  

Here are two vanilla javascript options:

这里有两个普通的javascript选项:

A.: Iterate over the object's keys and delete those having a falsey value.

一个。:遍历对象的键并删除具有falsey值的键。

var obj = {
  propA: true,
  propB: true,
  propC: false,
  propD: true,
};

Object.keys(obj).forEach(function(key) {
  if (!obj[key]) delete obj[key];
});

console.log(obj);

See Object.keys() and Array.prototype.forEach()

看到种()和Array.prototype.forEach()

B.: Iterate over the object's keys and add truthy values to a new object.

B。:遍历对象的键并向新对象添加truthy值。

var obj = {
  propA: true,
  propB: true,
  propC: false,
  propD: true,
};

var filteredObj = Object.keys(obj).reduce(function(p, c) {    
  if (obj[c]) p[c] = obj[c];
  return p;
}, {});

console.log(filteredObj);

See Object.keys() and Array.prototype.reduce()

看到种()和Array.prototype.reduce()

#3


1  

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 truthy for. So we can just use the identity() function as the callback, since it'll just return each property value.

函数的作用是:生成一个新对象,该对象包含回调返回truthy的属性。我们可以使用identity()函数作为回调,因为它只返回每个属性值。

#4


1  

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 _.identity as default you could also change your code to:

自Lodash v4以来,上面描述的功能已经转移到_.pickBy。_。身份默认情况下,您也可以将您的代码更改为:

var filtered = _.pickBy(obj);

See this JSBin for a working example.

查看此JSBin以获得一个工作示例。

#1


16  

Prior to 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,它将一个函数作为参数并返回一个对象,该对象只包含该函数返回truthy的键。所以你能做什么:

filtered = _.pick(obj, function(value, key) {return value;})

Or even more succinctly:

甚至更简洁地说:

filtered = _.pick(obj, _.identity)

Lodash 4.0

Lodash 4.0 split the _.pick function into _.pick, which takes an array of properties, and _.pickBy which takes a function. So now it'd be

Lodash 4.0拆分了_。选择函数_。pick,它取一个属性数组,还有_。它具有一个函数。所以现在会

filtered = _.pickBy(obj, function(value, key) {return value;})

Or, since _.pickBy defaults to using _.identity as it's second argument, it can just be written as:

或者,因为_。pickBy默认使用_。同一性作为第二个论点,可以写成:

filtered = _.pickBy(obj);

#2


4  

Here are two vanilla javascript options:

这里有两个普通的javascript选项:

A.: Iterate over the object's keys and delete those having a falsey value.

一个。:遍历对象的键并删除具有falsey值的键。

var obj = {
  propA: true,
  propB: true,
  propC: false,
  propD: true,
};

Object.keys(obj).forEach(function(key) {
  if (!obj[key]) delete obj[key];
});

console.log(obj);

See Object.keys() and Array.prototype.forEach()

看到种()和Array.prototype.forEach()

B.: Iterate over the object's keys and add truthy values to a new object.

B。:遍历对象的键并向新对象添加truthy值。

var obj = {
  propA: true,
  propB: true,
  propC: false,
  propD: true,
};

var filteredObj = Object.keys(obj).reduce(function(p, c) {    
  if (obj[c]) p[c] = obj[c];
  return p;
}, {});

console.log(filteredObj);

See Object.keys() and Array.prototype.reduce()

看到种()和Array.prototype.reduce()

#3


1  

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 truthy for. So we can just use the identity() function as the callback, since it'll just return each property value.

函数的作用是:生成一个新对象,该对象包含回调返回truthy的属性。我们可以使用identity()函数作为回调,因为它只返回每个属性值。

#4


1  

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 _.identity as default you could also change your code to:

自Lodash v4以来,上面描述的功能已经转移到_.pickBy。_。身份默认情况下,您也可以将您的代码更改为:

var filtered = _.pickBy(obj);

See this JSBin for a working example.

查看此JSBin以获得一个工作示例。