谁能告诉我如何更换下面的JavaScript code与循环? [英] Can someone tell me how to replace the following javascript code with a loop?

查看:134
本文介绍了谁能告诉我如何更换下面的JavaScript code与循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]); 
} 

是不一样的i设定为在循环中的岛我也曾尝试绑定技术从stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example

is not the same i as the i in the loop. I have also tried the binding technique from 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));
}

但是,这也导致错误也是如此。有人能告诉我如何使八线code到一个循环?

But that also resulted in an error as well. Could someone tell me how to make the eight lines of code into a loop?

推荐答案

就像上​​面说一个评论者,这几乎可以肯定是一个范围问题,但它很难说究竟是什么样的范围问题,没有看到周围code 。如果你有在JavaScript中范围的问题通常一个好主意,是包装尽可能在独立的功能,因为他们每个人都有自己的范围,可以作为封闭在一定程度上采取行动。也许尝试是这样的:

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. 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));

}

我不知道这是否会实际工作,即使它可能有一个更好的方式来构建关闭。如果你不熟悉立即调用函数前pressions,检查出的这个帖子,并考虑寻找一种方法来他们工作到code。他们是优秀的JavaScript中preventing范围的问题。

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 code与循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆