D.SZ
D.SZ

Reputation: 43

AngularJS search filter in array into object

I look ID in an array of objects JSON. Example JSON:

{
    "Przydzial": [{
            "M": "Cos",
            "Przydzialt": [{
                    "Name": "",
                    "Przydz": "tach_1",
                    "Cos": "Pod",
                    "Ha": "20",
                    "ID": "94"
                }, {
                    "Name": "B_K",
                    "Przydz": "lea",
                    "Cos": "Chea",
                    "HA": "8",
                    "ID": "78"
                }
            }]
    }]
}

Use in controller

var foo = { //my json };
var nowy = $filter('filter')(foo.Przydzialt, { ID:78});

result:

console.log(nowy); // undefined

json is correct - validated in JSLint.

Upvotes: 1

Views: 81

Answers (1)

Anna R
Anna R

Reputation: 151

As "$foo.Przydzial" is an array of objects, where every object has its "Przydzialt" attribute, you should execute $filter in a loop:

var newArray;

angular.forEach($foo.Przydzial, function (el) {
    newArray = $filter('filter')(el.Przydzialt, {ID: 78});
    console.log(newArray);
});

Upvotes: 1

Related Questions