Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over nested array of objects [duplicate]

I'm trying to iterate over this object containing a array of quests-objects. How would i be able to iterate over each key value pairs to for example return all quests which have the status "completed".

{
    "quests": [
        {
            "title": "A Rum Deal",
            "status": "COMPLETED",
            "difficulty": 2,
            "members": true,
            "questPoints": 2,
            "userEligible": true
        }
    ],
    "loggedIn": false
}
like image 914
Misha Karas Avatar asked Feb 22 '26 19:02

Misha Karas


1 Answers

For iterating you could use Array#forEach

object.quests.forEach(function (a) {
    if (a.status === "COMPLETED") {
        // do something with the data
    }
});

For returning a selection with completed task, you could use Array#filter

var completed = object.quests.filter(function (a) {
    return a.status === "COMPLETED";
});
like image 124
Nina Scholz Avatar answered Feb 25 '26 08:02

Nina Scholz