在javascript中返回for循环中的值 [英] Returning values out of for loop in javascript
本文介绍了在javascript中返回for循环中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下功能:
function getId(a){
var aL = a.length;
for(i = 0; i < aL; i++ ){
return a[i][2].split(":", 1)[0];
}
}
以及使用控制台时。函数中的log()
而不是 return
我得到循环中的所有值,也是如此文件撰写
。如何将这些值作为字符串访问以在我的代码的另一部分中使用?
and when using console.log()
within the function instead of return
I get all of the values in the loop, and the same goes for document.write
. How can I access these values as a string for use in another section of my code?
提前谢谢。
推荐答案
您可以使用 yield
,但这是不可能的。以下是您可以做的事情:
You can do that with yield
in newer versions of js, but that's out of question. Here's what you can do:
function getId(a){
var aL = a.length;
var values = [];
for(i = 0; i < aL; i++ ){
values.push(a[i][2].split(":", 1)[0]);
}
return values.join('');
}
这篇关于在javascript中返回for循环中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文