在javascript中返回for循环的值

时间:2022-11-16 07:31:51

I have the following function:

我有以下功能:

  function getId(a){
    var aL = a.length;
    for(i = 0; i < aL; i++ ){
      return a[i][2].split(":", 1)[0];
    }    
  }                          

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?

在函数中使用console.log()而不是返回时,我得到了循环中的所有值,document.write也是一样。如何将这些值作为字符串访问,以便在代码的另一部分中使用?

Thank you in advance.

提前谢谢你。

3 个解决方案

#1


8  

You can do that with yield in newer versions of js, but that's out of question. Here's what you can do:

在更新版本的js中,您可以使用yield,但这是毫无疑问的。你可以这样做:

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('');
}  

#2


1  

You gotta cache the string and return later:

你必须缓存字符串,然后返回:

function getId(a){
    var aL = a.length;
    var output = '';
    for(var i = 0; i < aL; i++ ){
       output += a[i][2].split(":", 1)[0];
    }    
    return output;
} 

#3


0  

  • The return statement breaks the loop once it is executed. Therefore consider putting the return statement outside the loop.
  • return语句在执行循环之后会中断循环。因此,考虑将return语句放在循环之外。
  • Since you want to return a string, you will create a variable and assign it to an empty string.(This is where will append/add results from the loop.)
  • 由于您希望返回一个字符串,您将创建一个变量并将其分配给一个空字符串。(在这里将附加/添加来自循环的结果。)
  • return the string variable.
  • 返回的字符串变量。

So final code will look like...

所以最终的代码看起来是…

function getId(a){
    var result = '';
    var aL = a.length;
    for(i = 0; i < aL; i++ ){
      result += a[i][2].split(":", 1)[0];
    } 
    return result;
  } 

#1


8  

You can do that with yield in newer versions of js, but that's out of question. Here's what you can do:

在更新版本的js中,您可以使用yield,但这是毫无疑问的。你可以这样做:

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('');
}  

#2


1  

You gotta cache the string and return later:

你必须缓存字符串,然后返回:

function getId(a){
    var aL = a.length;
    var output = '';
    for(var i = 0; i < aL; i++ ){
       output += a[i][2].split(":", 1)[0];
    }    
    return output;
} 

#3


0  

  • The return statement breaks the loop once it is executed. Therefore consider putting the return statement outside the loop.
  • return语句在执行循环之后会中断循环。因此,考虑将return语句放在循环之外。
  • Since you want to return a string, you will create a variable and assign it to an empty string.(This is where will append/add results from the loop.)
  • 由于您希望返回一个字符串,您将创建一个变量并将其分配给一个空字符串。(在这里将附加/添加来自循环的结果。)
  • return the string variable.
  • 返回的字符串变量。

So final code will look like...

所以最终的代码看起来是…

function getId(a){
    var result = '';
    var aL = a.length;
    for(i = 0; i < aL; i++ ){
      result += a[i][2].split(":", 1)[0];
    } 
    return result;
  }