将项目推入JavaScript中的多维树状结构 [英] Pushing item into a multi-dimensional tree like structure in JavaScript

查看:93
本文介绍了将项目推入JavaScript中的多维树状结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的对象的数组

I have an array of objects which goes on something like this

var obj = [{
    id: 23,
    name: 'Test1',
    children: [{
         id: 24,
         name: 'Test2,
         children: [..]
    },{
         id: 25,
         name: 'Test2,
         children: [..]
    }],
},{..}]

每个子代可以有多个子子代,因此基本上我想表示一种类似于结构的图,类似于 htmlparser .

Each children can have multiple sub children, so basically I am trying to represent a graph like structure, similar to the output of htmlparser.

我需要这样的功能:

function(nodeId, json){}

该函数将需要使用nodeId在树中找到json对象,并将json作为该父对象的子对象插入.那就是我被困住的地方.

The function will need to find the json object in the tree by using the nodeId and insert the json as a children of that parent object. Thats where I am stuck.

我试图编写这样的递归函数来搜索适当的节点,但是,当我必须将json插入实际的obj数组时,就会出现麻烦.

I tried writing a recursive function like this to search the appropriate node, however, the trouble arises when I have to insert the json into the actual obj array.

function findNode(nodeId, json, obj){
    if(obj.id == nodeId){
        obj.children.push(json);
    }
    else{
        for(var i=0; i<obj.children.length; i++){
            findNode(nodeId, json, obj.children[i]);
        }
    }
}

似乎json被插入到递归函数本地的obj中,而不是实际的根obj.如何将其插入父对象?

It seems the json gets inserted to the obj local to the recursive function, not the actual root obj. How can I can insert it to the parent obj?

推荐答案

首先,查看对象的结构,看来根对象的结构是错误的.根对象不能是您要拥有的对象的数组.要利用递归,您应该具有在所有级别上都相似的结构.

First of all, looking at your structure of an object, it looks like structure of your root object is wrong. Root object cannot be an Array of the objects that you intend to have. To take advantage of recursion, you should have structure which is similar on all levels.

您的函数实现几乎是正确的.

You function implementation is almost correct.

我只是希望您不要将JSON字符串作为json参数传递;如果是,则必须使用JSON.parse(json)

I just hope you are not passing JSON string as the json parameter; if you are, you have to parse it and make a JS object by using JSON.parse(json)

以下是更新的实现:

var obj = {
    id: 23,
    name: 'Test1',
    children: [{
         id: 24,
         name: 'Test2',
         children: []
    },{
         id: 25,
         name: 'Test2',
         children: []
    }],
};

var objToBePushed = {
    id: 26,
    name: 'Test3',
    children: [{
         id: 27,
         name: 'Test4',
         children: []
    }]
};

function findNode(nodeId, json, node){
    if(node.id == nodeId){
        node.children.push(json);
    }
    else{

        for(var i=0; i<node.children.length; i++){
            findNode(nodeId, json, node.children[i]);
        }
    }
}

findNode(24, objToBePushed, obj);
console.log(obj);

正在工作的JSFiddle: http://jsfiddle.net/ef3ewoag/2/

Working JSFiddle: http://jsfiddle.net/ef3ewoag/2/

这篇关于将项目推入JavaScript中的多维树状结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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