将一个多维JSON数组转换为另一个

时间:2018-12-10 19:07:20

标签: javascript arrays node.js json

我有以下输入对象

export function login (username, password) {
    return {
    type: types.LOGIN,
    payload: {
        request: {
        url: '/tokens/create',
        method: 'POST',
        data: {
            command:{
                Username: username,
                Password:password
                    }
                }
            }
        }
    }
}

我需要将其转换为以下json

{
    "id": 1,
        "isLeaf": false,
            "name": "New rule",
                "pid": 0,
                    "dragDisabled": true,
                        "children": [
                            {
                                "id": "new1",
                                "value": "data1",
                                "then": false,
                                "type": "set",
                                "forEach": false,
                                "pid": 1
                            },
                            {
                                "id": "new2",
                                "value": "data2",
                                "then": true,
                                "type": "if",
                                "forEach": false,
                                "pid": 1,
                                "children": [
                                    {
                                        "id": "new3",
                                        "type": "Then",
                                        "enableElse": true,
                                        "pid": "new2",
                                        "children": [
                                            {
                                                "id": "new5",
                                                "value": "data3",
                                                "then": false,
                                                "type": "fuzzy_search",
                                                "forEach": false,
                                                "pid": "new3"
                                            }
                                        ]
                                    },
                                    {
                                        "id": "new4",
                                        "type": "Else",
                                        "enableElse": true,
                                        "pid": "new2",
                                        "children": [
                                            {
                                                "id": "new6",
                                                "value": "data4",
                                                "then": false,
                                                "type": "return",
                                                "forEach": false,
                                                "pid": "new4"
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
}

我必须递归地遍历输入json数组的所有现有内部子数组以制定输出。 以下是该功能的部分实现的代码。

[
    {
        "id": "new1",
        "condition": "data1"
    },
    {
        "id": "new2",
        "condition": "data2",
        "then": [{
            "id": "new5",
            "condition": "data3"
        }],
        "else": [{
            "id": "new6",
            "condition": "data4"
        }]
    }
]

final输出数组具有一个条件键,该条件键绑定来自输入json的值键和“ then”键,该键包含多个连续的内部子数组,这是“ if”对象类型的成功条件。输出中的“ else”键也是如此

我发现很难递归调用相同的函数来生成所需的输出。当childs数组中有深层嵌套时,就会出现此问题。感谢您的帮助。

0 个答案:

没有答案