Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON object as follows:

{"email":null,"fullList":true,"listOfSomething":[
{
"contactEmailAddress": "[email protected]",
"somethingId": 11060767,
"other": "whatever"
},
{
"contactEmailAddress": "[email protected]",
"somethingId": 8499447,
"other": "whatever"
}, {
"contactEmailAddress": "[email protected]",
"somethingId": 3234664,
"other": "whatever"
},  {
"contactEmailAddress": "[email protected]",
"somethingId": 3233245,
"other": "whatever"
}
]
,"numOfResults":22}

In angular, I want to filter this list by an array of selected ID's, to match the key "SomethingID", within the key "listOfSomething"

My array of ID's to match against is formatted as:

[16199615,16199619]

My current filter just returns the entire list, unfiltered:

.filter('somethingFilter', ['somethingListService', function(somethingListService) {
domainsSelected = somethingListService.getSomethingSelected();
return function(array) {
    return array.filter(function(item) {
       return somethingSelected.indexOf(item.somethingId) === -1;
    });
};
}]);

I've tried so many different methods, but can find no examples where an array of numbers is matched against a nested JSON object, that has no numeric indexing.

share|improve this question
    
Davin - my mistake, was trying to obfuscate company sensitive code before posting question and forgot to change a variable name. –  Matthew Trow Dec 17 '14 at 14:49

1 Answer 1

up vote 2 down vote accepted

Here is a working example, of what I think you want to achieve: http://jsfiddle.net/martinczerwi/kdmqvw19/3/

I've noticed, your filter returns true if the ID is not found, that's why the whole array is dumped. Also the ID array's name was wrong. Should probably be:

return domainsSelected.indexOf(item.somethingId) !== -1;

In my example I've added an ID from the data to the filter IDs.

share|improve this answer
    
That's perfect - can't believe I didn't spot that obvious mistake, been working on the code for too long! - Thanks martinczerwi ! –  Matthew Trow Dec 17 '14 at 14:48
    
You're welcome, please accept the answer if it works for you ;) –  martinczerwi Dec 19 '14 at 10:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.