Query Master Query Master - 5 months ago 54
AngularJS Question

Filter apply in multidimensional array object with child node of response - Angular JS

I have result of JSON array object and need to apply in filter with

response_id
. This key is exist in child of response.

Helps are definitely appreciated.

My current implementation attempts to do something like this:



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">





Desired Result

[
{
"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"
}
]
}
]

Answer

So you filter with 23764,23765 and you want an array of all questions that contain one response object with id 23764 AND one response object with id 23765, and these should be the only response objects inside the returned results nested response array.

There are plenty of ways to write a custom filter function for this.

Here is one example:

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);

You probably want to add some argument checks and it can probably be optimized, but it should get you started at least.

Might also need to change the logic based on if you want copies of the original data or references.

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

Comments