无法在JSON对象上使用D3.Values()

发布于 2025-02-06 13:03:01 字数 450 浏览 3 评论 0原文

我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-02-13 13:03:01

d3.值等于object.values,当我看到您传递两个参数时,仅采用一个参数,这就是为什么错误的原因。我已经完成了一个示例片段供您获取最小值,对于每个项目对象,您可以将其映射以获取索引0的属性1值(您可以使用d3.values再次执行此操作)。您将获得最低属性1。注意:如果您不在下面进行ParseInt,那么您的数据将根据字符串IE 100提供最低限度,而不是2(照顾好)

,可以通过用indexof(propertyName)替换0索引来更好地答案。一系列属性。您可以尝试一下。

更新:d3.Values在D3V7中不支持,因此请使用对象。

const data =
{
 "Item1":{
   "Property1": "151",
   "Property2": "12",
 },
 "Item2":{
   "Property1": "21",
   "Property2": "22",
 },
 "Item3":{
   "Property1": "12",
   "Property2": "452",
 },
  "Item4":{
   "Property1": "100",
   "Property2": "4",
 }
}

const min = d3.min(Object.values(data).map(function(d) {
    return parseInt(Object.values(d)[0]);}));

console.log(min);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.4.4/d3.min.js" integrity="sha512-hnFpvCiJ8Fr1lYLqcw6wLgFUOEZ89kWCkO+cEekwcWPIPKyknKV1eZmSSG3UxXfsSuf+z/SgmiYB1zFOg3l2UQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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

const data =
{
 "Item1":{
   "Property1": "151",
   "Property2": "12",
 },
 "Item2":{
   "Property1": "21",
   "Property2": "22",
 },
 "Item3":{
   "Property1": "12",
   "Property2": "452",
 },
  "Item4":{
   "Property1": "100",
   "Property2": "4",
 }
}

const min = d3.min(Object.values(data).map(function(d) {
    return parseInt(Object.values(d)[0]);}));

console.log(min);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.4.4/d3.min.js" integrity="sha512-hnFpvCiJ8Fr1lYLqcw6wLgFUOEZ89kWCkO+cEekwcWPIPKyknKV1eZmSSG3UxXfsSuf+z/SgmiYB1zFOg3l2UQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文