Loop Through Object Searching For Property And Modify Original Object on Success

Viewed 184
var myObj = 
    data: {
    {
    chairId: "CHAIRCHROME009",    
    relatedOrders: [
        {
            someProp: 45423234,
            data : {
                firstOrder: {},
                relatedOrders: [
                    {
                        someProp: 5757587,
                        data : {
                            firstOrder: {},
                            relatedOrders: [ ],
                            notifications: [
                                {
                                    notificationId: "CHAIR-0909FF",
                                    latestNotification: {                                
                                        latestNotificationAction: "New",
                                        priority: "High",                                
                                    }
                                }
                            ],
                            relations: [ ]
                        }
                    }
                ],
                relations: [ ]
            }
        }
    ],
    relations: [ ]
}
}

How can i endlessly loop through an object containing arrays and objects, essentially searching for a notifications property.

In the example above, notifications is 2 levels deep in to myObj, however it could be 1 level or 100 levels. notifications will always be found under relatedOrders I then need to determine the value of notifications.latestNotification.latestNotificationAction

If no notifications are found, null or false should be returned.

I have tried:

function isThereRelatedOrders(myObj) {
    for(var k in myObj) {
        var v = myObj[k];

        if (k === "relatedOrders") {                
            isThereData(v[0]);
        }
    }
}

function isThereData(relatedOrder) {
    for(var y in relatedOrder) {
        var t = relatedOrder[y];

        if (y === "data") {                
            isThereNotification(t);
        }
    }
}

function isThereNotification(t) {
    for(var x in t) {            
        var j = t[x];

        if (x === "notifications") {
            console.lig('notifications found');
            if(j[0].latestNotification.latestNotificationAction  === "New") {                
                console.log('is new alert');
                // Add a property to my original myObj root level of alert: new
            }
            else {                
                console.log('is old alert');
                // Add a property to my original myObj root level of alert: old
            }
        }
        else if(x === "relatedOrders") {
            alert('SUB relatedOrders found');
            isThereRelatedOrders(j[0]);
        }
        else {
            alert('no notifications found');
        }
    }
}

isThereRelatedOrders(myObj);

With the above, once my code has got to calling isThereRelatedOrders() for the second time, it never hits the if statement.. I guess its becuase is now need to look for .data??

With the above executed, i would want something like this to be retunred:

var myObj = {
    chairId: "CHAIRCHROME009",
    alert: "New"
    ////////
3 Answers
Related