Javascript 在D3.js中使用数据函数时获取下一个或上一个结果

Javascript 在D3.js中使用数据函数时获取下一个或上一个结果,javascript,d3.js,charts,Javascript,D3.js,Charts,我正在使用图表库d3.js,我正在循环浏览一些数据,例如 thresholds = plot.select('.thresholds').selectAll('.threshold').data(chart.Thresholds); …然后做一些类似的事情: thresholds.attr('height', function(data) { // How can I access the data.whatever values in here from the previous or

我正在使用图表库d3.js,我正在循环浏览一些数据,例如

thresholds = plot.select('.thresholds').selectAll('.threshold').data(chart.Thresholds);
…然后做一些类似的事情:

thresholds.attr('height', function(data) {
  // How can I access the data.whatever values in here from the previous or next result in the data set?
});
在上面的示例中,是否有方法可以访问数据集中的上一个或下一个结果?

selection.attr(名称[,值]) 如果指定了值,则将具有指定名称的属性设置为 选定元素上的指定值,并返回此值 选择。如果该值为常量,则所有元素的值都相同 属性值;否则,如果值是函数,则为 按顺序为每个选定元素求值,并传递 当前基准(d)、当前指数(i)和当前组 (节点),并将其作为当前DOM元素(节点[i])。这个 然后使用函数的返回值设置每个元素的属性。 空值将删除指定的属性

您可以使用索引访问上一个或下一个:

thresholds.attr('height', function(data, i) {

   if(i>0) {
     var myvar = chart.Thresholds[i-1]
   } else {
    // Handle first case
   }

   if(i<(chart.Thresholds.length-1)) {
     var myothervar = chart.Thresholds[i+1]
   } else {
    // handle last case
   }

});
thresholds.attr('height',函数(数据,i){
如果(i>0){
var myvar=图表阈值[i-1]
}否则{
//办案
}

如果(iThanks)。但是,谁否决了这一点,谁就可以解释为什么这是一个糟糕的解决方案?