最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get loop value outside the loop - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 15, 2017 at 14:01 Osama XäwãñzOsama Xäwãñz 4372 gold badges9 silver badges22 bronze badges 5
  • 4 put console.log(result) inside the loop?? – Evochrome Commented Feb 15, 2017 at 14:02
  • *which are (1 to 10) * - you have 0 - 10 – RomanPerekhrest Commented Feb 15, 2017 at 14:04
  • The whole point of loops is to iterate! – ibrahim mahrir Commented Feb 15, 2017 at 14:04
  • i wanted the result outside the loop not inside the loop – Osama Xäwãñz Commented Feb 15, 2017 at 14:06
  • have an hidden element say an input. set the value of it inside the loop with your value desired. call the change event along for the input element. Add a event listener for the change of input and get that value which is obviously outside the loop. – Khaleel Commented Feb 15, 2017 at 14:14
Add a comment  | 

5 Answers 5

Reset to default 12

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

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

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
}

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

var result=[];

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

发布评论

评论列表(0)

  1. 暂无评论