简体   繁体   English

d3.js中的嵌套数组

[英]nested arrays in d3.js

I have seen questions about dealing with “nested json” in d3.js and have seen that the proposed solutions are mostly to “flatten” the data (meaning creating a new variable and pushing in the data in desired level) Here two examples: 我已经看到了有关在d3.js中处理“嵌套json”的问题,并且已经看到建议的解决方案主要是“拉平”数据(意味着创建新变量并将数据按所需级别压入),这里有两个示例:

My question is : can one use one of the various array functions to access the level of the data without creating a new variable ? 我的问题是:是否可以使用各种数组函数之一来访问数据级别而无需创建新变量? Handling arrays 处理数组

I have an array which looks like that: 我有一个看起来像这样的数组:

var dataset = 
 {"directed": false, 
  "graph": [], 
  "nodes": 
 [
 {"Region": "X15", "Group": "EU", "id": "BE"}, 
{"id": "FR"}, 
{"id": "BG"}, 
{"id": "DK"}, 
{"id": "HR"}, 
{"id": "DE"}
], 
"links": 
 [
 {"source": 0, "target": 0, "weight": 0}, 
 {"source": 0, "target": 1, "weight": 130}, 
 {"source": 0, "target": 2, "weight": 1}, 
 {"source": 0, "target": 3, "weight": 36}, 
 {"source": 0, "target": 4, "weight": 4}, 
 {"source": 0, "target": 5, "weight": 117}
 ], 
 "multigraph": false}

I have tried the following 我尝试了以下

and Iwould like to access the weight (calculating its maximum with d3.max) 并且我想访问权重(使用d3.max计算其最大值)

dataset.links // gives me link 
dataset.links[0].weight // gives me the weight of first but can't get the index to work

when using the array functions I can't get elements lower than that 当使用数组函数时,我无法获得低于该值的元素

 d3.entries(dataset)
 d3.values(dataset.links)

what am I missing ? 我想念什么?

I hope the question makes sense. 我希望这个问题有意义。

PS: if one has to flatten the array, is there any advantage of doing it in javascript or is it the same if I create the array (eg in python) and pass it to javascript PS:如果必须将数组弄平,那么在javascript中做数组有什么好处,或者如果我创建数组(例如在python中)并将其传递给javascript,是否一样

After reading over your post again, I think I know what your trying to do. 再次阅读您的文章后,我想我知道您的尝试。

If you want to access the weights column with something like d3.max , you can use an accessor function. 如果要使用诸如d3.max类的d3.max访问权重列,则可以使用访问器功能。

An accessor function specifies how you want to look through an item in the array. 访问器函数指定您如何查看数组中的项目。 In your case, you want a function that looks at the weight field in each link in dataset.links . 在您的情况下,您需要一个函数来查看dataset.links中每个链接中的weight字段。 With the link as an input, this function is quite simple: 使用链接作为输入,此功能非常简单:

function getWeight(link){return link.weight}

We can then use this function as the accessor function to get the max of the weight fields in dataset.links , as follows: 然后,我们可以将此函数用作访问器函数,以获取dataset.links最大weight字段,如下所示:

var maxWeight = d3.max(dataset.links, getWeight)

This uses the general form of the d3.max command, d3.max(array [,accessorFcn]) . 这使用d3.max命令的一般形式d3.max(array [,accessorFcn])

I've set up a fiddle that does this and then alerts the max weight. 我已经设置了一个提琴 ,然后提醒最大重量。

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

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