简体   繁体   English

有人可以告诉我如何用循环替换以下javascript代码吗?

[英]Can someone tell me how to replace the following javascript code with a loop?

dataBase[0].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[0].columnline]); });

dataBase[1].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[1].columnline]); });

dataBase[2].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[2].columnline]); });

dataBase[3].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[3].columnline]); });

dataBase[4].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[4].columnline]); });

dataBase[5].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[5].columnline]); });

dataBase[6].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[6].columnline]); });

dataBase[7].valueline = d3.svg.line()
.x(function(d) { return x(d["Date"]); })
.y(function(d) { return y(d[dataBase[7].columnline]); });

I have tried the statement: 我已经尝试过以下声明:

for (var i = 0; i < dataBase.length; i++) {

dataBase[i].valueline = d3.svg.line()
    .x(function(d) { return x(d["Date"]); })
    .y(function(d) { return y(d[dataBase[i].columnline]); });

}

but that did not work because the i in 但这没有用,因为我在

function(d) { 
  return y(d[dataBase[i].columnline]); 
} 

is not the same i as the i in the loop. i与循环中的i不同。 I have also tried the binding technique from stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example 我还尝试了stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example的绑定技术

function createfunc(count) {
    return function(d) {
        return y(d[dataBase[count].columnline]);
    };
}
for (var i = 0; i < dataBase.length; i++) {

dataBase[i].valueline = d3.svg.line()
    .x(function(d) { return x(d["Date"]); })
    .y(createfunc(i));
}

But that also resulted in an error as well. 但这也导致了错误。 Could someone tell me how to make the eight lines of code into a loop? 有人可以告诉我如何将八行代码变成一个循环吗?

As a commenter above said, this is almost certainly a scope issue, but it's difficult to say exactly what kind of scope issue without seeing the surrounding code. 正如上面的评论者所述,这几乎肯定是一个范围问题,但是很难在不查看周围代码的情况下确切地说出哪种范围问题。 One generally good idea if you're having scope issues in JavaScript is to wrap as much as possible in standalone functions, since they each have their own scope and can act as closures to a degree. 如果您在JavaScript中遇到范围问题,通常一个好主意是将尽可能多的包装在独立函数中,因为它们每个都有自己的范围,并且在一定程度上可以充当闭包。 Maybe try something like: 也许尝试类似:

function xFunc(d) {
    return function (d) { return x(d["Date"]) };
}

function yFunc(d, i) {
    return function (d) { return y(d[dataBase[i].columnline]) };
}

for (var i = 0; i < dataBase.length; i++) {

    dataBase[i].valueline = d3.svg.line()
        .x(xFunc(d))
        .y(yFunc(d, i));

}

I have no idea if that will actually work, and even if it does there's probably a better way to build that closure. 我不知道这是否会真正起作用,即使这样做,也可能有更好的方法来构建该闭包。 If you're not familiar with Immediately Invoked Function Expressions, check out this post , and consider finding a way to work them into your code. 如果您不熟悉立即调用的函数表达式,请查看这篇文章 ,并考虑找到一种将它们应用于代码的方法。 They are excellent for preventing scope issues in JavaScript. 它们非常适合防止JavaScript中的范围问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM