Javascript Node.js for() 循环在每个循环中返回相同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10234898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js for() loop returning the same values at each loop
提问by Tristan Dubé
I'm making this really simple application to help me explore nodejs and I have a particular handler that generates HTML code based on the top10 messages in my database. The snippet I'm having problem with loops through the messages and call the function generating HTML and appends the result to my html string.
我正在制作这个非常简单的应用程序来帮助我探索 nodejs,并且我有一个特定的处理程序,可以根据我的数据库中的前 10 条消息生成 HTML 代码。我遇到问题的代码段遍历消息并调用生成 HTML 的函数并将结果附加到我的 html 字符串。
function CreateMessageboard(BoardMessages){
var htmlMessageboardString = "";
[... Console debug code ...]
for(var i = 0; i < BoardMessages.length;i++){
(function(){
var j = i;
console.log("Loading message %d".green, j);
htmlMessageboardString += MessageToHTMLString(BoardMessages[j]);
})();
}
}
I think my problem is due to either Javascript's way of handling loops, related to closures from what I read and this is what I tried using above or the async way nodejs handles my function. Right now the 10 results are well returned from the db but the last message is processed at every loop.
我认为我的问题是由于 Javascript 处理循环的方式,与我阅读的闭包有关,这是我在上面尝试使用的方法,或者是 nodejs 处理我的函数的异步方式。现在 10 个结果很好地从数据库返回,但最后一条消息在每个循环中处理。
I also tried, instead of doing var j = i, to take value i as function parameter and pass it in the closure and it returned the same results anyway.
我也尝试过,而不是做 var j = i,将值 i 作为函数参数并将其传递到闭包中,无论如何它返回相同的结果。
I have a feeling I'm missing critical knowledge to solve my problem, can I get light on this matter ?
我觉得我缺少解决问题的关键知识,我可以了解一下这个问题吗?
Edit : I'm welcome to give any other info on the code, I'd post the whole git repo but people probably don't want to swim through a whole project to help me debug this issue so I posted the whole function in the comments to provide more context.
编辑:欢迎我提供有关代码的任何其他信息,我会发布整个 git 存储库,但人们可能不想浏览整个项目来帮助我调试此问题,因此我将整个功能发布在评论以提供更多背景信息。
回答by Joe
for(var i = 0; i < BoardMessages.length;i++){
(function(j){
console.log("Loading message %d".green, j);
htmlMessageboardString += MessageToHTMLString(BoardMessages[j]);
})(i);
}
That should work; however, you should never create a function in a loop. Therefore,
那应该有效;但是,您永远不应该在循环中创建函数。所以,
for(var i = 0; i < BoardMessages.length;i++){
composeMessage(BoardMessages[i]);
}
function composeMessage(message){
console.log("Loading message %d".green, message);
htmlMessageboardString += MessageToHTMLString(message);
}
回答by ming_codes
I would suggest doing this in a more functional style :P
我建议以更实用的风格来做这件事:P
function CreateMessageboard(BoardMessages) {
var htmlMessageboardString = BoardMessages
.map(function(BoardMessage) {
return MessageToHTMLString(BoardMessage);
})
.join('');
}
Try this
尝试这个