【问题标题】:Filter scope by categories true underscore按类别过滤范围 true 下划线
【发布时间】:2015-10-14 15:01:52
【问题描述】:

我只需要过滤类别中的帖子,例如 desenvolvimento: true。

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

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

已解决!!! JsBin 中的示例

遵循 JSBin: https://jsbin.com/turodayaqe/1/edit?html,output

【问题讨论】:

  • Y我们的代码只在第一级迭代,_.each 没有迭代嵌套的键,所以你的“post.tax.categories.desenvolvimento”没有达到

标签: angularjs underscore.js


【解决方案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
        }
      });
    }
  });

});

但我不太确定你想做什么。

【讨论】:

  • 我需要的很简单,我只想获取具有“开发”类别的对象帖子。我正在按角度类别应用过滤器。
  • $scope.teste = .map(.map($scope.posts, 'tax'), 'categorias');这部分工作
【解决方案2】:

您可以使用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
    });

【讨论】:

  • 这个过滤功能直接任意作用域,我不知道,很好。一旦输出,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多