简体   繁体   English

使用JavaScript从数据库检索深层数据结构

[英]Retrive deep data stucture from database using JavaScript

I have a data structure like below: 我有一个如下的数据结构:

Task(id,name,subTasks[Task])

But the problem is the subTasks can contain Tasks which have another subTasks. 但是问题在于子任务可以包含具有另一个子任务的任务。 This can run to very deep like this: 这样可能会非常深入:

Task1 Contains SubTask1

SubTask1 contains it's sub tasks

and you can understand this can be run to very deep. 并且您可以理解,这可以深入到很深的地方。

I can retrieve these data from a database tables. 我可以从数据库表中检索这些数据。 But how can I store this in a data structure in java script. 但是如何将其存储在Java脚本的数据结构中。 Using for loops without knowing the deep is useless and not a elegant way. 在不了解深度的情况下使用for循环是没有用的,而且不是一种优雅的方法。 What would be the best data structure and data traversal way? 最佳的数据结构和数据遍历方式是什么?

If you want to transfer arbitrarily-nested data structures to and from some other program (perhaps written in some other language), JSON format is a good format, which can be easily read or written in JavaScript -- see JSON objects in JavaScript . 如果您想与其他程序(可能用其他语言编写)之间来回传送任意嵌套的数据结构,JSON格式是一种很好的格式,可以轻松地用JavaScript读取或编写-请参见JavaScript中的JSON对象 But there is no need for JSON if you're going to create and use such structures entirely internally in a single JavaScript program. 但是,如果您要完全在单个JavaScript程序内部创建和使用这样的结构,则不需要JSON。

The general technique to traverse arbitrarily nested data structures is recursion . 遍历任意嵌套的数据结构的通用技术是递归 (Some people claim that "recursion" is one of the 2 most important things to learn about programming, even though they are not needed in 90% of written code -- Spolsky , Tim Bray , Jeff Atwood , Wenham , Sooriamurthi , etc. ) (有些人声称,“递归”是学习编程的2件最重要的事情之一,尽管并不需要他们在90%的成文法典- 斯波斯基蒂姆·布雷杰夫·阿特伍德威翰Sooriamurthi等)

There are many stackoverflow questions that discuss recursion in JavaScript . 有很多stackoverflow问题讨论JavaScript中的递归 You will get much better answers if you read a few of them, write a little bit of code, test it, and then post a short bit of code -- it doesn't have to be perfect. 如果你看过他们几个,你会得到更好的答案,写的代码一点点,测试它,然后张贴简短的代码位-它并不一定是完美的。

// define the "Task" class
function Task(id, name, depth){
    this.id = id;
    this.name = name;
    this.depth = depth;
    this.subTask = []; // Use "[]" rather than "{}" so it works with "forEach".
    this.stringify_name_and_all_subtasks = function(depth){
        var return_string = this.depth + ":" + this.name + " ";
        // If "forEach" doesn't work for you,
        // use the "Compatibility" code in
        // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
        this.subTask.forEach( function(entry){
            return_string += entry.stringify_name_and_all_subtasks(depth);
        });
        return return_string;
    };
};

All too often, programmers try to force the data to fit into a strict hierarchy, with nested data structures. 程序员常常试图通过嵌套数据结构来强制数据适应严格的层次结构。 ( DOM manipulation and Javascript Data Structures: Flat or Structured? , Hierarchy Considered Harmful , etc. ) DOM操作和Javascript数据结构:扁平还是结构化? 认为有害的层次结构等。)

However, non-nested structures are better when you don't have a pure hierarchy. 但是,当您没有纯层次结构时,非嵌套结构会更好。 Sometimes they work better even when you do have a pure hierarchy -- such as when you want to look up things without traversing the entire structure. 有时,即使您只有一个纯粹的层次结构,它们也可以更好地工作-例如,当您想查找事物而不遍历整个结构时。 For example, "What tasks depend on the sub-task named 'rotate stock' ?" 例如,“哪些任务取决于名为“库存”的子任务?

Perhaps one of the many alternatives to hierarchy might work better in your case. 在您的情况下,也许层次结构的许多替代方法之一可能会更好。 For example, an associative array: 例如,一个关联数组:

<script>
"use strict";
// global task list
var tasklist_by_id = [];
var task_name_to_id = {};

// add a new task to global tasklist
function add_task(id, name, supertaskid){
    tasklist_by_id[ id ] = {"id":id, "name":name, "supertaskid":supertaskid};
    /* in other words,
    var new_task = {};
    new_task.id = id;
    new_task.name = name;
    new_task.supertaskid = supertaskid;
    tasklist_by_id[ id ] = new_task;
    */
    task_name_to_id[ name ] = id;
};

function ancestors_of( task_id ){
    if( task_id ){
        var my_name =  tasklist_by_id[ task_id ].name;
        var my_supertaskid = tasklist_by_id[task_id].supertaskid;
        var my_ancestors = ancestors_of( my_supertaskid );
        var ancestor_string = " -- " + my_name + my_ancestors;
        return ancestor_string;
    }else{
        return ".";
    };
};

function test(){
    add_task( 1, "Main task #1", 0 );
    add_task( 2, "Subtask 1", task_name_to_id[ "Main task #1" ] );
    add_task( 3, "Sub-subtask 1", task_name_to_id[ "Subtask 1" ] );
    add_task( 4, "Another Subtask of Main task 1", task_name_to_id[ "Main task #1" ] );
add_task( 5, "Sub-sub-subtask 1", task_name_to_id[ "Sub-subtask 1" ] );
    add_task( 6, "rotate_stock", task_name_to_id["Sub-sub-subtask 1" ])

    // What task is the parent task(s) of "rotate_stock" ?

    var some_task_name = "rotate_stock";
    var some_task_id = task_name_to_id[some_task_name];
    var ancestors = ancestors_of( some_task_id );

    alert("The ancestry line of " + some_task_name + " is " + ancestors);
}

test();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM