I need to make a filter that searches the following array of objects and filters out those objects whose ID
value is equal to a Message
value in any of those objects in the array. Also the filter should return only objects with level
equal to 1
. I've already managed to filter the level
property, but I can't seem to figure out how to do the rest.
Let's consider an example: ID: 001
contains in its Message
006
, so I want to filter out all objects, whose ID
is 006
.
$scope.myData = [
{
"ID" : "001",
"Message" : "006",
"level":"1"
},
{
"ID" : "002",
"Message" : "test test test test1",
"level":"1"
},
{
"ID" : "003",
"Message" : "test test test test",
"level":"1"
},
{
"ID" : "004",
"Message" : "test test test test",
"level":"1"
},
{
"ID" : "005",
"Message" : " My Test",
"level":"1"
},
{
"ID" : "006",
"Message" : "",
"level":"1"
},
{
"ID" : "007",
"Message" : "next level",
"level":"2"
}
];
})
And filter:
app.filter('filterData3', function () {
return function (data) {
var dataToBePushed = [];
data.forEach(function (resultData) {
if (resultData.level == 1)
dataToBePushed.push(resultData);
});
return dataToBePushed;
}
});
Assuming that what you want is to NOT include in your filtered array the elements which ID is contained in another element's Message, you could write a filter like the following:
.filter('filterData3', function () {
return function (data) {
var dataToBePushed = [];
var messages =[];
data.forEach(function (resultData) {
if (resultData.level == 1 && (messages.indexOf(resultData.ID) == -1)) {
messages.push(resultData.Message);
dataToBePushed.push(resultData);
}
});
return dataToBePushed;
}})
Hope this works for you