javascript – 如何使用Underscore.js过滤器与对象?

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用Underscore.js过滤器与对象?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个像这样的对象:
  1. > Object
  2. > Rett@site.com: Array[100]
  3. > pel4@gmail.com: Array[4]
  4. > 0
  5. id : 132
  6. selected : true
  7. > 1
  8. id : 51
  9. selected : false

等等..

如何使用下划线_.filter()仅返回选中的项目=== true?

我从来没有必要使用_.filter()进入图层.就像是

  1. var stuff = _.filter(me.collections,function(item) {
  2. return item[0].selected === true;
  3. });

谢谢

解决方法

如果要从选择为true的任何电子邮件地址中提取所有数组元素,可以这样迭代:
  1. var selected = [];
  2.  
  3. for (email in emailLists) {
  4. selected.concat(_.filter(emailLists[email],function (item) {
  5. return item.selected === true;
  6. }));
  7. }

如果您只想拉出选择了所有元素的数组,您可能会执行以下操作:

  1. var stuff = _.filter(me.collections,function(item) {
  2. return _.all(item,function (jtem) {
  3. jtem.selected === true;
  4. });
  5. });

猜你在找的JavaScript相关文章