2015-10-14 116 views
0

我只需要过滤类别内的帖子,例如desenvolvimento:true。按类别过滤范围true下划线

我试图使用下划线,但无法让我回到所有的帖子,只需要包含此过滤器的帖子。

$scope.filter = 'desenvolvimento'; 
$scope.teste = _.where($scope.posts, 'desenvolvimento'); 

解决!例如,在JsBin

沿用了JSBin: https://jsbin.com/turodayaqe/1/edit?html,output

+1

你的代码只迭代的第一级,_.each ISN” t迭代嵌套键,所以你的“post.tax.categories。desenvolvimento”没有达到 –

回答

0

您可以使用Array.prototype.filter或下划线的_.filter,只是检查属性链的存在,而最终的结果(demo):

$scope.filter = 'artigos'; 

$scope.resolve = $scope.posts.filter(function (item) { 
     return !!(item.tax && item.tax.categorias && item.tax.categorias[$scope.filter]); // go over the props chain, if anything is missing it will return false 
    }); 
+0

THIS过滤器功能直接任何范围,我不知道,非常好。一旦输出,谢谢 –

+1

不客气:) –

1

在你的情况,你会需要这样的东西:

_.each(obj, function (v1, k1) { 
    _.each(v1, function(v2){ 
    if(typeof v2 === "object") { 
     _.each(v2, function(v3){ 
     console.log(v3); 
     if(v3 === value) { 
      // do something 
     } 
     }); 
    } 
    }); 

}); 

但我不太清楚你想要做什么。

+0

我需要的非常简单,我只想把对象只包含“development”类别的帖子,我正在申请 –

+1

$ scope.teste = _.map(_。map($ scope.posts,'tax'),'categorias'); T他的部分工作 –