【问题标题】:Filter apply in multidimensional array object with child node of response in Angular JS过滤器应用于Angular JS中具有响应子节点的多维数组对象
【发布时间】:2016-10-03 10:41:34
【问题描述】:

我有数组对象的结果,需要用response_id 申请过滤器。此键存在于响应的子级中。如果我使用 23764、23765 进行搜索,我想要所有具有至少 2 个响应、id 为 23764 和 23765 的 Question 对象,这些是我想要在响应数组中唯一的两个响应对象。

如果我只使用 23764 进行搜索,我想要所有具有该 id 的响应对象的 Question 对象,并且只需要数组中的该响应对象。

我当前的实现尝试做这样的事情:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$filter) {
    
    $scope.data = [
    {
        "id": "1341",
        "question": "What about this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Every thing "
            },
            {
                "response_id": "23765",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
            
        ]
    },
    {
        "id": "1342",
        "question": "What dislike about this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Nothing"
            },
            {
                "response_id": "23765",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
        ]
    },
    {
        "id": "1343",
        "question": "What suggestion(s) this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Nothing "
            },
            {
                "response_id": "23765",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
        ]
    }
  ];
  
  var res = ($filter('filter')($scope.data, {response:{response_id:'23764,23765'}}, true));
  console.log(res);
  
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

期望的结果

[
{
    "id": "1341",
    "question": "What about this chaneel?",
    "response": [
        {
            "response_id": "23764",
            "comment": "Every thing "
        },
        {
            "response_id": "23765",
            "comment": "No"
        }
    ]
},
{
    "id": "1342",
    "question": "What dislike about this chaneel?",
    "response": [
        {
            "response_id": "23764",
            "comment": "Nothing"
        },
        {
            "response_id": "23765",
            "comment": "No"
        }
    ]
},
{
    "id": "1343",
    "question": "What suggestion(s) this chaneel?",
    "response": [
        {
            "response_id": "23764",
            "comment": "Nothing "
        },
        {
            "response_id": "23765",
            "comment": "No"
        }
    ]
}
]

【问题讨论】:

  • 您能描述一下您的目标吗?您使用 23764,23765 作为过滤器值。例如,您想要orand 逻辑吗?
  • @tasseKATT 需要and
  • @tasseKATT 我有逗号分隔值,该值已从视图中传递,需要在过滤器中应用and 而不是or
  • 我假设您不希望每个对象都有response_id = 2376423765,因为没有。您希望每个大对象都有具有匹配 id 的响应对象,所以至少有两个响应对象?听起来像是一个非常具体的过滤器。
  • @tasseKATT 是的,你是对的,需要特定的过滤器来匹配对象中的 id 并相应地过滤。

标签: javascript angularjs json filter


【解决方案1】:

因此,您使用23764,23765 进行过滤,并且您想要一个包含一个响应对象的所有问题数组,其中包含一个 ID 为 23764 的响应对象AND 一个 ID 为 23765 的响应对象,这些应该是仅返回结果嵌套响应数组内的响应对象。

有很多方法可以为此编写自定义过滤器函数。

这是一个例子:

var myFilter = function(collection, values) {

  values = values.replace(' ', '').split(',');

  var result = [];

  collection.forEach(function(question) {

    var questionMatches = [];

    question.response.forEach(function(r) {

      if (values.indexOf(r.response_id) > -1) questionMatches.push(r);
    });

    if (questionMatches.length !== values.length) return;

    var questionCopy = angular.extend({}, question);

    questionCopy.response = questionMatches;

    result.push(questionCopy);
  });

  return result;
};

var filterValues = '23764, 23765';

var result = myFilter($scope.data, filterValues);

您可能想要添加一些参数检查,并且可能会对其进行优化,但至少应该可以帮助您入门。

如果您想要原始数据或引用的副本,可能还需要更改逻辑。

演示: http://plnkr.co/edit/r6a5qbw4JJ6Zc5eFopLT?p=preview

【讨论】:

  • 是的,您是对的,原始数据的逻辑需要进行一些更改。第二次这将过滤过滤的数据
  • 不是每次要过滤都传原始数据,还是我误会了你?
  • 我处理好了……现在它完全满足了我的要求……再次感谢
【解决方案2】:

您可以使用角度 forEach 循环来删除对象。试试这个。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$filter) {
    
    $scope.data = [
    {
        "id": "1341",
        "question": "What about this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Every thing "
            },
            {
                "response_id": "237657",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
            
        ]
    },
    {
        "id": "1342",
        "question": "What dislike about this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Nothing"
            },
            {
                "response_id": "23765",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
        ]
    },
    {
        "id": "1343",
        "question": "What suggestion(s) this chaneel?",
        "response": [
            {
                "response_id": "23764",
                "comment": "Nothing "
            },
            {
                "response_id": "237654",
                "comment": "No"
            },
            {
                "response_id": "23766",
                "comment": ".."
            }
        ]
    }
  ];
   $scope.result = [];
  angular.forEach($scope.data,function(d){
      angular.forEach(d.response,function(res,index){
          if(res.response_id == "23766"){
            d.response.splice(index,1);
             console.log(d.response);
            }
        })
    })
//console.log($scope.data);
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <pre>{{data| json}}</pre>
  </div>

【讨论】:

  • 过滤后我们如何避免 23766 响应 id
  • 你到底想要什么??
  • 我已经编辑了我的问题,请查看我想要的结果
  • 我不使用静态方法,因为我有数组对象的动态结果
猜你喜欢
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多