Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript——分离层次数组或JSON对象的一个分支_Javascript_Arrays_Json - Fatal编程技术网

JavaScript——分离层次数组或JSON对象的一个分支

JavaScript——分离层次数组或JSON对象的一个分支,javascript,arrays,json,Javascript,Arrays,Json,我有一个JSON对象,它是一个嵌套数组,其形式如下: { "name": "Math", "children": [ { "name": "Trigonometry", "children": [ { "name": "Right Triangles and an Introduction to Trigonometry",

我有一个JSON对象,它是一个嵌套数组,其形式如下:

{
    "name": "Math",
    "children": [
        {
            "name": "Trigonometry",
            "children": [
                {
                    "name": "Right Triangles and an Introduction to Trigonometry",
                    "children": [
                        {
                            "name": "The Pythagorean Theorem",
                            "children": [
                                {
                                    "name": "The Pythagorean Theorem",
                                    "size": 30
                                },
                                {
                                    "name": "Pythagorean Triples",
                                    "size": 52
                                },
                                {
                                    "name": "Converse of the Pythagorean Theorem",
                                    "size": 13
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "Algebra",
            "children": [
                {
                    "name": "Equations and Functions",
                    "children": [
                        {
                            "name": "Variable Expressions",
                            "children": [
                                {
                                    "name": "Evaluate Algebraic Expressions",
                                    "size": 26
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
整个阵列实际上要大得多,可以看到。我正在使用它使用D3.js(或者其他库)构建交互式图形和图表。因为它太大了,所以我希望能够按分支拆分阵列。换言之,要雕刻出阵列的特定分支


例如,是否有一种方法可以直接拉出节点“三角函数”及其所有子节点?还是“代数”和它的所有孩子?然后,“三角”或“代数”将成为新的根节点或父节点。

没有内置的方法来完成类似的操作,尽管关于JSON查询语言的注释可能会为您提供正确的解决方法

事实上,问题在于,您的数据结构使其很难使用。如果代替

{
    name: "key",
    children: [...]
}
你刚刚做到了

{
    "key": [...]
}
然后只需执行
myObject[“key”]
即可获得所需的数组

例如:

var math = {
    "Trigonometry": {
        "Right Triangles and an Introduction to Trigonometry": {
            "The Pythagorean Theorem": {
                "The Pythagorean Theorem": 30,
                "Pythagorean Triples": 52,
                "Converse of the Pythagorean Theorem": 13
            }
        }
    },
    "Algebra": {
        "Equations and Functions": {
            "Variable Expressions": {
                "Evaluate Algebraic Expressions": 26
            }
        }
    }
};
var trigonometry = math["Trigonometry"];
var expressionsAndFunctions = math["Algebra"]["Expressions and Functions"];

作为奖励,这要短得多

阵列的拼接函数应该可以做到这一点。这样做的目的是删除给定索引处的元素并返回它

如果你只是想要一个到某个特定分支的快捷方式,你就不能使用它吗

var trig=树[‘三角法’]

去那里。这不会改变原始对象,buy将为您提供一种更简单的方式来访问内部节点。

检查您是否询问如何在javascript代码中找到合适的子对象?