无法在JSON对象上使用D3.Values()
我使用的是D3的最新版本,需要获取看起来像这样的JSON对象的嵌套属性的最小值和最大值:
const data =
{
"Item1":{
"Property1": "11",
"Property2": "12",
},
"Item2":{
"Property1": "21",
"Property2": "22",
}
}
要获得property1
的最小值,我知道我无法直接使用d3.min
在一个对象上,因此我尝试通过以下几个以下操作将其首先转换为数组:
const min = d3.min(d3.values(data, (d) => +d.Property1))
vscode告诉我没有错误,但是在控制台上,它告诉我“值”不是一个功能。我应该怎么办?
I am using the latest versionof d3 and need to get the minimum and maximum values of a nested property of a JSON object which looks like this:
const data =
{
"Item1":{
"Property1": "11",
"Property2": "12",
},
"Item2":{
"Property1": "21",
"Property2": "22",
}
}
To get the minimum value of Property1
I know that I cannot directly use d3.min
on an object so I tried to convert it first into an array by doing the following:
const min = d3.min(d3.values(data, (d) => +d.Property1))
VScode tells me that there are no errors but on the console it tells me that "values" it's not a function. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
d3.值等于object.values,当我看到您传递两个参数时,仅采用一个参数,这就是为什么错误的原因。我已经完成了一个示例片段供您获取最小值,对于每个项目对象,您可以将其映射以获取索引0的属性1值(您可以使用d3.values再次执行此操作)。您将获得最低属性1。注意:如果您不在下面进行ParseInt,那么您的数据将根据字符串IE 100提供最低限度,而不是2(照顾好)
,可以通过用indexof(propertyName)替换0索引来更好地答案。一系列属性。您可以尝试一下。
更新:d3.Values在D3V7中不支持,因此请使用对象。
d3.values is equivalent of Object.values which takes only one parameter while I see you are passing two parameters , so that is why an error. I have done an example snippet for you to get the minimum, for each Item object, you can map it to get property1 value which is at index 0 ( you do this again by using d3.values). You get minimum of the property1. Note: If you dont do parseInt below, your data will give minimum based on string i.e. 100 will be lesser than 2 ( take care of that)
There can be a better answer to that by replacing 0 index with indexOf(propertyname) if you have an array of Properties. You can give a try.
UPDATE: d3.values is not supported in d3v7, so use Object.values instead