gpt4 book ai didi

javascript - 在 .json 中查找最大值和最小值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:03 25 4
gpt4 key购买 nike

对于我尝试使用 d3.js 处理的图表,我需要为轴设置比例。

图表的数据存储在结构如下的 Json 文件中:

[{
"id" : "AustraliaNewZealand" ,
"year" : [1990,1992,1994,1993,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010],
"value":[477790,485825,487296,488885,502585,511718,526594,538967,550556,563572,577171,579126,591635,599216,604267,608954,615853,623685,618961,614920]
},
{
"id":"CentralEurope",
"year":[1990,1992,1994,1993,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010],
"value":[1548736,1334452,1313088,1283248,1314709,1360546,1343907,1285511,1237278,1251872,1244069,1233778,1284639,1297510,1317861,1359787,1396681,1361445,1278731,1343044]
},
{
"id":"EasternEurope",
"year":[1990,1992,1994,1993,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010],
"value":[4418516,3530464,3287987,2925644,2775181,2672238,2540975,2495036,2513372,2515375,2540796,2544848,2598148,2637682,2622241,2709974,2714204,2740248,2565213,2680226]
},
{
"id":"NorthAmerica",
"year":[1990,1992,1994,1993,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010],
"value":[6750754,6821412,6948829,7059001,7166869,7386971,7460485,7509719,7573562,7790060,7675762,7710685,7769154,7896824,7918461,7841686,7966277,7751508,7277713,7493948]
},
{
"id":"WesternEurope",
"year":[1990,1992,1994,1993,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010],
"value":[4365949,4290345,4222425,4221029,4264725,4353056,4290057,4310736,4251628,4260565,4306230,4278128,4333237,4337031,4305560,4267515,4209481,4125479,3841580,3924831]
}]

我想做的是在嵌套的各种数组之间迭代以获取年份和值的最小值和最大值,但我不知道如何获取这些值。我设法使用以下方法获取单个数组的值:

var xScale = d3 .time.scale()
.domain((d3.extent(dataset[0].year)))
.range([margin*2, (width - margin)]);
var yScale = d3 .scale.linear()
.domain([0, d3.max(dataset[0].value)])
.range([(height - margin), margin]);

其中 _dataset_ 是回调函数的标识符,但我想要的是获取所有数组之间的最小值和最大值(例如最小值在 array3 中,数组 1 中的最大值)。我想我应该使用 for 循环,但我不知道我应该把它放在哪里以及如何设置它以查看所有数组。

最佳答案

如果我们在这里使用一些函数式编程而不是 for 循环,那将非常有趣:

d3.extent(dataset.reduce(function(a,b) { return a.concat(b.year) }, []))

您可以对值执行相同的操作。

关于javascript - 在 .json 中查找最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com