【问题标题】:dessolve Object structure解决对象结构
【发布时间】:2016-03-04 12:19:52
【问题描述】:

我像这样从我的 REST-Api 中获取一个对象

var obj = {
            array1: [
                {
                    "elm": 1, children: [
                    {
                        "elm": 2, children: [
                        {
                            "elm": 3, children: [
                            {
                                "elm": 4, children: [
                                {"elm": 5}
                            ]
                            }
                        ]
                        }
                    ]
                    }
                ]
                }
            ],
            array2: [
                {
                    "elm": 6, children: [
                    {
                        "elm": 7, children: [
                        {
                            "elm": 8, children: [
                            {
                                "elm": 9, children: [
                                {"elm": 10}
                            ]
                            }
                        ]
                        }
                    ]
                    }
                ]
                }
            ]
        };

由于更改了接口并且无法访问 api 及其结构构建,我必须将对象分解为以下内容:

    var newObj = {
        array1: [
            {
                "elm": 1, children: [
                {"elm": 2},
                {"elm": 3},
                {"elm": 4},
                {"elm": 5}
            ]
            }
        ],
        array2: [
            {
                "elm": 6, children: [
                {"elm": 7},
                {"elm": 8},
                {"elm": 9},
                {"elm": 10}
            ]
            }
        ]
    };

有没有一种更简单的方法可以在不遍历每个孩子的情况下解散对象?

【问题讨论】:

  • “我必须将对象分解成这样”你确定吗?
  • 是的,我是。我们得到了子类别的子类别的子类别。现在我们只有 1 个子类别应该显示所有子类别。对于接下来的步骤,我们必须使用所有子类别的 ID 来获取我们想要的项目。

标签: javascript angularjs json object


【解决方案1】:

也许这对你有用......

var obj = { array1: [{ "elm": 1, children: [{ "elm": 2, children: [{ "elm": 3, children: [{ "elm": 4, children: [{ "elm": 5 }] }] }] }] }], array2: [{ "elm": 6, children: [{ "elm": 7, children: [{ "elm": 8, children: [{ "elm": 9, children: [{ "elm": 10 }] }] }] }] }] },
    result = function (object) {

        function dig(a) {
            this.push({ elm: a.elm });
            Array.isArray(a.children) && a.children.forEach(dig, this);
        }

        var r = {};
        Object.keys(object).forEach(function (k) {
            object[k].forEach(function (a) {
                var array = [];
                r[k] = r[k] || [];
                r[k].push({ elm: a.elm, children: array });
                Array.isArray(a.children) && a.children.forEach(dig, array);
            });
        });
        return r;
    }(obj);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多