How to get loop value outside the loop

我只是一个虾纸丫 提交于 2020-04-10 05:52:00

问题


How to get the loop values outside the loop below example only prints the last value, For example if i wanted to print the loop results.

var result;

    for (var i=0; i < 10; i++) {
          result = i;
    }

    console.log(result);

Now how can i get the iterated values of the loop which are (1 to 10) in the console, because now i will only print the last value which is 10.


回答1:


Put the log statement inside the loop where you set the value.

var result;

for (var i=0; i < 10; i++) {
    result = i;
    console.log(result);
}

If you only want one output statement, you can concatenate your results before logging:

var result = "";

for (var i=0; i < 10; i++) {
    result += i + " ";
}

console.log(result);

This will output 0 1 2 3 4 5 6 7 8 9 10




回答2:


If you really want to log outside of the loop, wich is quite unnecessary in my opinion, may use an array? :

var result=[];

for (var i=0; i < 10; i++) {
      result.push(i);
}
console.log(...result);

http://jsbin.com/gogeluhavi/edit?console

If you want result make to log magically, you may uses setters and a Proxy, so called Observables.

Enter result=10 into this console, ive implemented a Observable for you: http://jsbin.com/xacujabuwo/edit?console ; You could also paste your for loop...




回答3:


The Above answears are correct but I would like to clear it up for you so you understand it too.
In your original code you have declared the variable "result" outside the loop, then at each iteration a value is assigned to your variable. So that first time around "result" = 0 , second loop and the "result" = 1 and so on.

When the the for loop is finished it reads the next line which is your console.log() statment with the variable "result". The last time the variable "result" was modified was inside the for loop at the 10th iteration and thats where you let it equal to the iterator,therefore the value of iterator is 11.

In order to display something at every iteration, the console.log() must be placed inside the loop. Check below example

 var result;
for (var i=0; i < 10; i++) {
    result = i;
    console.log(result); // prints at every iteration
}



回答4:


Since you didnt add jQuery tag I used only javascript.

Add a input hidden tag

<input id="idOfInput" style="visibility:hidden" type="text">

Set the desired value to the input

for (var i=0; i < 10; i++) {
result = i;
document.getElementById('idOfInput').value = result;
document.getElementById('idOfInput').change(); //note change() is to trigger the event
}

Add change event listener and get the value set in loop

var input = document.getElementById('idOfInput');
input.addEventListener('input', function()
{
    console.log('input changed to: ', input.value); //you get the value here
});

hope it helps



来源:https://stackoverflow.com/questions/42251470/how-to-get-loop-value-outside-the-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!