使嵌套的 JSON Javascript [英] Making Nested JSON Javascript

查看:30
本文介绍了使嵌套的 JSON Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为森伯斯特图制作嵌套的 JSON,但是我遇到了一些问题.正如您在下面的这个小提琴中看到的那样,共享父名称的孩子并未应用于所有内容?我希望 No Virtual 具有与 Virtual Parent 相同的属性.但是因为它们的名字相同,所以我遇到了问题?

有没有办法检查树的根是否相同?这样它每次都可以不同,但​​是它会越来越多地嵌套并且名称的这些选择是相同的.

http://jsfiddle.net/cre96uf3/2/

//创建一个名称:节点映射var dataMap = data.reduce(function(map, node) {地图[节点名称] = 节点;返回地图;}, {});//创建树数组无功树 = [];data.forEach(函数(节点){//添加到父级var parent = dataMap[node.parent];如果(父母){//如果不存在则创建子数组(parent.children || (parent.children = []))//将节点添加到子数组.push(节点);} 别的 {//parent 为空或缺失树推(节点);}});//显示我们有什么d3.select('body').append('pre').text(JSON.stringify(tree, null, ' '));

解决方案

d3.nest 让您的生活比这更轻松.

这是一个非常有用的工具来习惯嵌套:http://bl.ocks.org/shancarter/raw/4748131/>

否则请在此处阅读相关信息:http://www.d3noob.org/2014/02/grouping-and-summing-data-using-d3nest.html

祝你好运,

I am trying to make nested JSON for the sunburst diagram however I am having some trouble. As you can see in this fiddle below that the children that share a parent name are not being applied to everything? I want the No Virtual to have the same Attributes as the Virtual Parent. However because they are the same name I am running into a problem?

Is there someway to check if the root of the tree are the same? And that way it can be different every time, however it is going to get more and more nested and these choices of the name are the same.

http://jsfiddle.net/cre96uf3/2/

 // create a name: node map
var dataMap = data.reduce(function(map, node) {
map[node.name] = node;
return map;
}, {});

 // create the tree array
 var tree = [];
data.forEach(function(node) {
// add to parent
var parent = dataMap[node.parent];
if (parent) {
    // create child array if it doesn't exist
    (parent.children || (parent.children = []))
        // add node to child array
        .push(node);
} else {
    // parent is null or missing
    tree.push(node);
}
 });

 // show what we've got
 d3.select('body').append('pre')
     .text(JSON.stringify(tree, null, '  '));

解决方案

There is d3.nest which makes your live much easier than this.

This is a very helpful tool to get used to nesting: http://bl.ocks.org/shancarter/raw/4748131/

Otherwise read up about it here: http://www.d3noob.org/2014/02/grouping-and-summing-data-using-d3nest.html

Good luck,

Kim

这篇关于使嵌套的 JSON Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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