简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM