JavaScript 递增数组中的数字

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

JavaScript Incrementing a number in an array

javascript

提问by Sudantha

I want to increment a value in a array when a link is pressed in JavaScript

我想在 JavaScript 中按下链接时增加数组中的值

i Used the following code

我使用了以下代码

<script type="text/javascript">

 var i=0;
 var numbers = new Array();


 function go(val){

 numbers[i]=val;
 i++;

 alert(numbers[i]);

 }

</script>

Called the Function like this

像这样调用函数

<a href='javascript:go(1)' </a> 

but always the alert prompts me 'undefined'

但警报总是提示我“未定义”

回答by Jonathon Faust

The alert is correct -- before you do your alert, you incremented i. You're looking at the nextelement after the one you just entered.

警报是正确的——在你做警报之前,你增加了i。您正在查看刚刚输入的元素之后的下一个元素。

After calling the method once, your array looks like this:

调用一次该方法后,您的数组如下所示:

numbers[0] = 1;
numbers[1] = undefined;

and i == 1.

i == 1

After calling it again, the array looks like:

再次调用后,数组如下所示:

numbers[0] = 1;
numbers[1] = 1;
numbers[2] = undefined;

and i == 2.

i == 2

Hopefully you can see that this method will alwaysalert undefined

希望你能看到这个方法会一直提醒undefined

回答by Pointy

That's because you increment "i"

那是因为你增加了“i”

i++;

right beforeyou put up the alert! Thus "i" will alwuays refer to the nextarray slot to use, not the one you just populated.

你发出警报之前!因此,“i”始终指的是下一个要使用的阵列槽,而不是您刚刚填充的阵列槽。

You could change the alert to use "i-1"

您可以更改警报以使用“i-1”

alert(numbers[i - 1]);

回答by meder omuraliev

You are setting numbers[0] = 1and then incrementing iwhich becomes 1so alert(numbers[1])is undefined, because it isundefined.

您设置numbers[0] = 1,然后递增i变成1这样alert(numbers[1])是不明确的,因为它不确定的。

Do the alertbefore you increment. Also, use onclickor even better unobtrusively attach the event handlers in JS, not in the HTML.

alert你增加了。此外,onclick在 JS 中而不是在 HTML 中使用甚至更好地不显眼地附加事件处理程序。

回答by Andrzej Doyle

Yes, it does that because you:

是的,这样做是因为您:

  1. Create a completely empty array, and a pointer at 0.
  2. When the function is called, you set the current pointer value to whatever was passed in...
  3. ...and then increment the pointer, so it's now pointing past the end of all the elements.
  4. Nowyou look at the element in the array that's being pointed at, which hasto be undefinedbecause of the way you're managing the ipointer.
  1. 创建一个完全空的数组,以及一个指向 的指针0
  2. 调用该函数时,您将当前指针值设置为传入的任何值...
  3. ...然后增加指针,所以它现在指向所有元素的末尾。
  4. 现在你看看元素就是BEING指着阵列,这undefined因为这样你所管理的i指针。

What were you hoping for this to do, by the way?

顺便说一句,你希望这样做是为了什么?

回答by keV

The question doesn't even match the code... or the code doesn't match the question?

问题甚至与代码不匹配……或者代码与问题不匹配?

"I want to increment a value in a array"

“我想增加数组中的一个值”

Your code is not incrementing the value, it's incrementing the index!

您的代码不是在增加值,而是在增加索引!

function go(val){
  numbers[i]=val;
  i++;
}

(where iis the index of the next undefined array element) is just the same as

(其中i是下一个未定义数组元素的索引)与

numbers.push(val);

and if you need ito equal what will be the index of the next undefined array element then

如果你需要i等于下一个未定义数组元素的索引,那么

i = numbers.length;

To increment the valueyou would have to first have numeric values for some array elements; then your function would need the index of which value to increment

要增加该值,您必须首先获得某些数组元素的数值;那么您的函数将需要要递增的值的索引

var numbers = [0,0,0,0];

function go(i){
  numbers[i]++;
}

// testing

go(1);
go(3);
go(1);

alert(numbers);

will show 0,2,0,1

将显示 0,2,0,1

But if your entire goal is to put a value into a new element on the end of an array then just use .push()

但是,如果您的整个目标是将一个值放入数组末尾的新元素中,那么只需使用 .push()

and .length will tell you how many elements there are; there is no need to increment an i

.length 会告诉你有多少元素;没有必要增加i