问题描述
我有一个像这样的数组数组。
I have an array of arrays like so.
data = [
[
{x: 1, y: 40},
{x: 2, y: 43},
{x: 3, y: 12},
{x: 4, y: 60},
{x: 5, y: 63},
{x: 6, y: 23}
], [
{x: 1, y: 12},
{x: 2, y: 5},
{x: 3, y: 23},
{x: 4, y: 18},
{x: 5, y: 73},
{x: 6, y: 27}
], [
{x: 1, y: 60},
{x: 2, y: 49},
{x: 3, y: 16},
{x: 4, y: 20},
{x: 5, y: 92},
{x: 6, y: 20}
]
];
我可以找到带有嵌套d3的 data 的最大y值。 max()调用:
I can find the maximum y value of data with a nested d3.max() call:
d3.max(data, function(d) {
return d3.max(d, function(d) {
return d.y;
});
});
我正在努力了解此代码的实际工作方式。我知道d3.max()函数的第二个参数指定了一个访问器函数-但我对调用d3.max()两次与访问器函数的确切关联感到困惑。
I'm struggling to understand how this code actually works. I know the second argument of the d3.max() function specifies an accessor function - but I'm confused into how exactly calling d3.max() twice relates with the accessor function.
我想我要问的是有关JavaScript如何解释此代码的演练。我已经在控制台上浏览了它,但不幸的是它没有帮助。
I guess what I'm asking for is a walkthrough of how javascript interprets this code. I've walked through it on the console but it didn't help unfortunately.
推荐答案
有时,这全都与变量的命名有关:
Sometimes it's all about the naming of the variables:
// the outer function iterates over the outer array
// which we can think of as an array of rows
d3.max(data, function(row) {
// while the inner function iterates over the inner
// array, which we can think of as an array containing
// the columns of a single row. Sometimes also called
// a (table) cell.
return d3.max(row, function(column) {
return column.y;
});
});
您可以在此处找到d3.max函数的源代码:
You can find the source code for the d3.max function here: https://github.com/d3/d3.github.com/blob/8f6ca19c42251ec27031376ba9168f23b9546de4/d3.v3.js#L69
这篇关于将d3.max与数组数组嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!