简化的JavaScript for循环

Cha*_*apo 0 javascript arrays sorting recursion object

我有几个层次的深Javscript对象。除了最终级别(我需要排序的数组)以外,所有级别都是对象。

到目前为止,我的代码如下所示:

for (let group in objRes) {
    if (objRes.hasOwnProperty(group)) {
        for (let type in objRes[group]) {
            if (objRes[group].hasOwnProperty(type)) {
                for (let name in objRes[group][type]) {
                    if (objRes[group][type].hasOwnProperty(name)) {
                        for (let tenor in objRes[group][type][name]) {
                            if (objRes[group][type][name].hasOwnProperty(tenor)) {
                                objRes[group][type][name][tenor] = objRes[group][type][name][tenor].sort((x,y)=>x.date>y.date);
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

水平(grouptypenametenor)都是字符串和最后一级数组成员如下所示:{date:'2019-12-25',value:35}

所以objRes看起来像

{
group1:
    {type1:
        {name1:
            {tenor1:[{date:'2019-12-25',value:35},...],
         name2 :{...}
         }
    },
    {type2 :{...}},
group2:{...}
}

Run Code Online (Sandbox Code Playgroud)

有什么聪明的方法可以简化这个过程吗?

您可以假定级别数未知。

T.J*_*der 5

您可以为此使用递归函数。很难根据问题中的信息给出确切的例子,但是例如:

function process(obj) {
    // Loop through the values of the own properties of the object
    for (const value of Object.values(obj)) {
        // Is this the termination condition?
        if (Array.isArray(value)) { // <== A guess at the condition, adjust as needed
            // We've reached the bottom
            value.sort((x, y) => x.date.localeCompare(y.date)); // <== Note correction, you can't just return the result of `>`
        } else {
            // Not the termination, recurse
            process(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

实时示例,对数据进行猜测:

function process(obj) {
    // Loop through the values of the own properties of the object
    for (const value of Object.values(obj)) {
        // Is this the termination condition?
        if (Array.isArray(value)) { // <== A guess at the condition, adjust as needed
            // We've reached the bottom
            value.sort((x, y) => x.date.localeCompare(y.date)); // <== Note correction, you can't just return the result of `>`
        } else {
            // Not the termination, recurse
            process(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
function process(obj) {
    // Loop through the values of the own properties of the object
    for (const value of Object.values(obj)) {
        // Is this the termination condition?
        if (Array.isArray(value)) { // <== A guess at the condition, adjust as needed
            // We've reached the bottom
            value.sort((x, y) => x.date.localeCompare(y.date)); // <== Note correction, you can't just return the result of `>`
        } else {
            // Not the termination, recurse
            process(value);
        }
    }
}

const objRes = {
    group1: {
        type1: {
            name1: {
                tenor1: [
                    {date: '2019-12-23', value: 35},
                    {date: '2019-12-25', value: 32},
                    {date: '2019-12-24', value: 30},
                ]
            },
            name2 :[]
        },
        type2: {}
    },
    group2: {}
};
process(objRes);
console.log(JSON.stringify(objRes, null, 4));
Run Code Online (Sandbox Code Playgroud)

一些注意事项:

  1. 您可以使用来避免使用for-in/ hasOwnProperty组合Object.values

  2. 您可以使用来遍历这些值for-of

  3. 必须有一些终止条件来告知函数何时到达“底部”。在该示例中,我使用了Array.isArray因为您在最后一级进行排序。

  4. Array.prototype.sort 直接修改数组,您无需使用其返回值。

  5. 您传递给的函数sort必须返回一个负数,0或一个正数,而不是布尔值(更多信息请参见)。由于您的date值似乎是yyyy-MM-dd形式上的字符串,因此您可以使用localeCompare它(因为碰巧是这种格式,字典比较也是日期比较)。