如何将for循环每次迭代的结果存储到数组中(Javascript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19801995/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:51:13  来源:igfitidea点击:

How to store the result of each iteration of a for loop into an array (Javascript)

javascriptarraysfor-loopiteration

提问by Seeeyonnn

This question could also be (depending on how you look at it) - How to use a for loop to do a basic math operation that adds up the sum of the multiples of 3

这个问题也可能是(取决于你如何看待它) - 如何使用 for 循环进行基本的数学运算,将 3 的倍数的总和相加

So I am doing Problem 1 of Project Euler.

所以我正在做 Project Euler 的问题 1。

I've (already) hit a steel-reinforced wall. I could use some help - with just a specific portion of the problem please (I am not looking for just an answer to the problem, but rather, an answer to my specific problem so I can carry on with trying to solve the remainder problem using any answer you guys provide me here)...

我(已经)撞到了钢筋墙。我可以使用一些帮助 - 请只使用问题的特定部分(我不是在寻找问题的答案,而是寻找我的特定问题的答案,这样我就可以继续尝试使用你们在这里给我的任何答案)...

Anyways.

无论如何。

I (think I) created a for loop that will give me the multiples of 3. However, I'm trying to store the result of each iteration of the for-loop so that it adds up to the sum of those multiples i.e. I'm trying to store the result from each iteration of the loop - whether it be into an array or into a variable that takes the sum of the multiples - it doesn't matter to me - I wouldn't mind learning both methods.

我(认为我)创建了一个 for 循环,它会给我 3 的倍数。但是,我试图存储 for 循环每次迭代的结果,以便它加起来就是这些倍数的总和,即 I'我试图存储循环每次迭代的结果 - 无论是存储到数组中还是存储到取倍数总和的变量中 - 对我来说无关紧要 - 我不介意学习这两种方法。

I'm sure this sounds kind of confusing so let me paint my picture w/ an example...

我敢肯定这听起来有点令人困惑,所以让我用一个例子来画我的照片......

I have a for-loop:

我有一个 for 循环:

for (i = 1; i <= 3; i++) {
var x = 0;
x += (i * 3);
WHAT DO I DO NEXT????

^ So I would think that this gives me x with a value of 3 upon the 1st iteration of the loop, x with a value of 9 on the 2nd loop, and x with a value of 18 on the final loop. That's correct, right? (if this were returning 18 I don't think I would need to store the values of each iteration into an array)

^ 所以我认为这在循环的第一次迭代中给了我 3 的值,在第二次循环中 x 的值为 9,而在最后一个循环中 x 的值为 18。这是正确的,对吧?(如果返回 18,我认为我不需要将每次迭代的值存储到数组中)

1st iteration:

第一次迭代:

i = 1; x = 0

Original equation...

原方程...

(i * 3) + x            

So...

所以...

(1 * 3) + (x = 0) = 3

So after completion of the 1st loop, x has a value of 3, right??? (Question: How would I store this value of x (which is 3) - how would I store it in an array while in this stage of the for loop?)

所以在第一个循环完成后,x 的值为 3,对吧???(问题:我将如何存储 x 的这个值(即 3)——在 for 循环的这个阶段我如何将它存储在一个数组中?)

2nd iteration of loop:

循环的第二次迭代:

i = 2; x = 3

(2 * 3) + (x = 3) = 9     

(same question as before: how would I add this value to an array?)

(与之前相同的问题:如何将此值添加到数组中?)

3rd iteration:

第三次迭代:

i = 3; x = 9

(3 * 3) + (x = 9) = 18      

Q: shouldn't this be the final value of x upon completion of the for loop??? For some reason, when I run the code, the final value of x is 9, and not 18

问:这不应该是 for 循环完成后 x 的最终值吗???出于某种原因,当我运行代码时,x 的最终值是 9,而不是 18

So, basically I am trying to add the sum of the 3 values...But what do I do next? I thought my for loop would add the result of the equation after each loop to x, but instead of ending up w/ 18 (the sum of 3 + 6 + 9), x's value was 9???

所以,基本上我正在尝试将 3 个值的总和相加……但是接下来我该怎么做?我以为我的 for 循环会在每次循环后将方程的结果添加到 x,但不是以 18(的总和3 + 6 + 9)结束,x 的值为 9???

Should I use an array? If so, I'm thinking I could add the return value to an array, but I'm not sure how to add the result of each iteration of the loop to an array. Maybe the following?...

我应该使用数组吗?如果是这样,我想我可以将返回值添加到数组中,但我不确定如何将循环的每次迭代的结果添加到数组中。也许以下?...

for (i = 1; i <= 3; i++) {
var array = [];
x = 0;
x += (i *3); 
array.push(x);
};

^ I tried running that in jfiddle, but it would only add the last value of x (9) into the array... So how would I add the value of x to an array after each iteration of the for loop??? And I'm not seeing what's wrong with my for-loop to where it's returning a value of x as 9?

^ 我尝试在 jfiddle 中运行它,但它只会将 x (9) 的最后一个值添加到数组中...那么在 for 循环的每次迭代之后我将如何将 x 的值添加到数组中???而且我没有看到我的 for 循环有什么问题,它将 x 的值返回为 9?

Also, I'm assuming the euler problems get significantly more difficult as we progress? If so, I've got a TON of work/practice to do....

另外,我假设随着我们的进步,欧拉问题会变得更加困难?如果是这样,我有大量的工作/实践要做....

THANKS IN ADVANCE...

提前致谢...

回答by p.s.w.g

Just create the array once, and push the result at each iteration to the array:

只需创建一次数组,并将每次迭代的结果推送到数组中:

var array = [];
for (i = 1; i <= 3; i++) {
    var x = 0;
    x += (i *3); 
    array.push(x);
}

Or for that matter, just use this:

或者就此而言,只需使用这个:

var array = [];
for (i = 1; i <= 3; i++) {
    array.push(i *3);
}

Or to simply get the sum of the factors, use this:

或者简单地得到因素的总和,使用这个:

var x = 0;
for (i = 1; i <= 3; i++) {
    x += i *3;
}

回答by StackSlave

You are declaring var x = 0and var array = []at every step of the loop, try something like:

您正在声明var x = 0var array = []在循环的每一步,尝试如下操作:

var array = [], x = 0;
for (i=1; i<4; i++){
  x += (i*3); 
  array.push(x);
}