递归创建JSON树,只在最深层次添加 [英] Recursively creating a JSON tree, adding only at the deepest level

查看:44
本文介绍了递归创建JSON树,只在最深层次添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个未知对象的JSON层次结构,所以必须递归处理.

I want to create a JSON hierarchy structure of unknown objects, so it must be handled recursively.

这是我的函数,其中 angular.element.isEmptyObject() 继承自 jQuery,而 angular.copy() 是一个创建对象(我使用的是 AngularJS).

Here's my function, where angular.element.isEmptyObject() is inherited from jQuery, and angular.copy() is a function that creates a deep copy of the object (I'm using AngularJS).

function recurseTree(tree, newKey, newId) {
    if(angular.element.isEmptyObject(tree)) {
        tree[newKey] = {_id: newId};
    } else {
        for(var key in tree) {
            if(typeof tree[key] == 'object') recurseTree(tree[key], newKey, newId);
            else tree[newKey] = {_id: newId};
        }
    }
    return angular.copy(tree);
}

现在运行:

var testT = {};
console.log(recurseTree(testT, 'a', '1'));
console.log(recurseTree(testT, 'b', '2'));
console.log(recurseTree(testT, 'c', '3'));
console.log(recurseTree(testT, 'd', '4'));
console.log(recurseTree(testT, 'e', '5'));

您会注意到第一个和第二个按预期返回:

You'll notice that the first and second ones return as expected:

{ 
    a: { 
        _id: '1',
        b: {
            _id: '2'
        }
    }
}

但第三个是我遇到麻烦的地方.

but the third one is where I run into trouble.

{ 
    a: { 
        _id: '1',
        b: {
            _id: '2',
            c: {
                _id: '3'
            }
        },
        c: {
            _id: '3'
        }
    }
}

我需要修复什么才能将 c 对象附加ONLY 作为 b 的子级,而不是作为 b 的子级一个?我被难住了.

What do I need to fix to get the c object appended ONLY as a child of b, rather than also as a child of a? I'm stumped.

这是一个正在运行的 JSFiddle,检查您的控制台以获取结果.http://jsfiddle.net/winduptoy/Mjq5D/2/

Here's a JSFiddle of it in action, check your console for the results. http://jsfiddle.net/winduptoy/Mjq5D/2/

推荐答案

试试这个:

function recurseTree(tree, newKey, newId) {
    if(angular.element.isEmptyObject(tree)) {
        tree[newKey] = {_id: newId};
        return;
    } 

    var child = null; // find current tree's child
    for(var key in tree) {
        if (key != '_id') {
            child = tree[key]; // found a child
            break;
        }
    }
    if (child) { // recursively process on child
        recurseTree(child, newKey, newId);
    } else { // no child, so just fill the tree
        tree[newKey] = {_id: newId};
    }
}

测试:

var testT = {};
recurseTree(testT, 'a', '1');
console.log(testT);  
recurseTree(testT, 'b', '1');
console.log(testT); 
recurseTree(testT, 'c', '1');
console.log(testT); 
recurseTree(testT, 'd', '1');
console.log(testT); 
recurseTree(testT, 'e', '1');
console.log(testT);

请注意,为了性能,我没有使用 angular.copy(tree).如果您不想更改树,请在将其传递给函数 recurseTree 之前复制它.请在 jsFiddle 上试用.

Please be noted that I have not used angular.copy(tree) for the performance's sake. If you don't want to change the tree, copy it before passing it to the function recurseTree. Please try it on jsFiddle.

这篇关于递归创建JSON树,只在最深层次添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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