循环通过对象搜索属性并在成功时修改原始对象 [英] Loop Through Object Searching For Property And Modify Original Object on Success

查看:61
本文介绍了循环通过对象搜索属性并在成功时修改原始对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

在上面的示例,通知是 myObj 的2级深度,但它可以是1级或100级。将始终在 relatedOrders 下找到通知然后我需要确定 notifications.latestNotification的值.latestNotificationAction

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

如果未找到通知, null false 。

我试过:

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

有了上述内容,一旦我的代码第二次调用isThereRelatedOrders(),它就永远不会点击if语句..我猜它现在需要寻找.data ??

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"
    ////////


推荐答案

此提案使用迭代和递归方法获取所需属性通知及其内容。如果找到,则将内容推送到结果数组中。如果找不到任何内容,则数组保持为空。

This proposal uses an iterative and recursive approach to get the wanted property notifications and their content. If one found, the content is pushed into the result array. If nothing is found, then the array remains empty.

function iter(o) {
    if (Array.isArray(o)) {
        o.forEach(iter);
        return;
    }
    if (typeof o === 'object') {
        Object.keys(o).forEach(function (k) {
            if (k === 'chairId') {
                chairId = o[k];
            }
            if (k === 'notifications') {
                o[k].forEach(function (a) {
                    if ('latestNotificationAction' in a.latestNotification) {
                        result.push({
                            chairId: chairId,
                            alert: a.latestNotification.latestNotificationAction
                        });
                    }
                });
            }
            iter(o[k]);
        });
    }
}

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: [] } },
    chairId,
    result = [];

iter(myObj);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

编辑:

一个解决方案,它尊重数据 relatedOrders 属性之间的切换。

A solution which respects the toggle between data and relatedOrders properties.

function iter(o, flip) {
    if (Array.isArray(o)) {
        o.forEach(function (a) {
            iter(a, flip);
        });
        return;
    }
    if (typeof o === 'object') {
        if ('chairId' in o) {
            chairId = o.chairId;
        }
        if (
            flip === 'relatedOrders' &&
            'notifications' in o &&
            0 in o.notifications &&
            'latestNotification' in o.notifications[0] &&
            'latestNotificationAction' in o.notifications[0].latestNotification
        ) {
            result.push({
                chairId: chairId,
                alert: o.notifications[0].latestNotification.latestNotificationAction
            });
            return;
        }
        flip in o && iter(o[flip], flipFlop[flip]);
    }
}

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: [] } },
    chairId,
    result = [],
    flipFlop = { relatedOrders: 'data', data: 'relatedOrders' };

iter(myObj, 'data');
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

这篇关于循环通过对象搜索属性并在成功时修改原始对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆