javascript 将数组值分配给变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16081952/
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
assigning an array value to a variable
提问by mrageh
I came across a problem which I did manage to solve but I honestly do not understand how this works.
我遇到了一个我确实设法解决的问题,但老实说,我不明白这是如何工作的。
var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
//My code
for (var i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
How does my array assign a value to my variable within the if statement?
我的数组如何在 if 语句中为我的变量赋值?
Why does this version not work?
为什么这个版本不行?
array[i] = largest;
I'm very new to programming and I believe that understanding fundamental things as small as this can help many beginners become competent programmers.
我对编程很陌生,我相信理解如此小的基本事物可以帮助许多初学者成为称职的程序员。
采纳答案by NPKR
if(array[i] > largest) {
largest = array[i];
}
array[i]
is 3
and largest
is 0
then if condition passes and
array[i]
is 3
and largest
is 0
then 如果条件通过并且
largest = array[i];
assign value right to left
now the largest
value is 3
.
从右到左赋值,现在largest
值为3
。
so next cycle array[i]
is 6
so next 6 > 3
true , it will again change the largest to 6
.
所以下一个周期array[i]
是6
这样下一个6 > 3
真的,它会再次改变largest to 6
。
like it will give you largest number.
喜欢它会给你最大的数字。
回答by EduG
I think you are mistaking the assignment statement in programming with the equality statement in mathematics.
我认为您将编程中的赋值语句与数学中的等式语句混淆了。
In your code, when you say largest = array[i] you are assigning to the variable 'largest' the value of the element on the ith position of the array. Notice here that the array is not changing any of its values in this assignment.
在您的代码中,当您说 large = array[i] 时,您将数组的第 i 个位置上的元素的值分配给变量 'largest'。请注意,在此分配中,数组没有更改其任何值。
On the other hand, array[i] = largest would try to assign to the ith position on the array the present value of the variable 'largest'.
另一方面,array[i] = maximum 会尝试将变量“largest”的当前值分配给数组中的第 i 个位置。
回答by Cyclopeus
When you assign a value to a variable, the variable has to be on the left of the assignment operator (the equals sign "=").
为变量赋值时,变量必须位于赋值运算符的左侧(等号“=”)。
This way has the variable on the left:
这种方式在左侧有变量:
largest = array[i];
It puts the array[i]value into the largestvariable, and so it works.
它将array[i]值放入最大的变量中,因此它可以工作。
This way is backwards:
这种方式是倒退的:
array[i] = largest;
It puts the largestvalue into the array[i]element, and your array ends up with a bunch of zeros in it.
它把最大的值到数组[我]元素,和你的阵列一堆在它的零结束。
回答by AurA
This code is trying to calculate the largest value in an array of values..
此代码试图计算值数组中的最大值。
Doing
正在做
array[i] = largest;
would modify the input itself which is undesired... you need to find the largest value from the given input.. and not modify the given input location to some largest value.
会修改不需要的输入本身......你需要从给定的输入中找到最大值......而不是将给定的输入位置修改为某个最大值。
That is why
因此
largest = array[i]
is correct.
是正确的。
回答by Yoarthur
I know this is an old thread, but i've this issue today and want to help others developers with proper source of knowledge, comming directly from Kyle Simpson.
我知道这是一个旧线程,但我今天遇到了这个问题,并希望通过直接来自凯尔辛普森的适当知识来源帮助其他开发人员。
Operator assignment has his own way to work. It calculate the statement of the right "=" to later asign it, to the left. This is very important to know how does it work.
操作员分配有他自己的工作方式。它计算右边“=”的语句,稍后将其赋值给左边。了解它是如何工作的非常重要。
Example:
例子:
var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
//My code
for (var i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
Here, largest = array[i];
这里, largest = array[i];
JS, is calculatethe statement of the rightand assing to the left
JS,就是计算右边和左边的语句
So have fun with the book and play.
所以尽情享受这本书和玩耍吧。