admin管理员组文章数量:1289508
Can someone please tell me why the length of secondArray
is 5 instead of 10?
The result I am looking for is for all elements to be popped off so the secondArray has an empty. However, it seems that only half of them are being popped off, even though I have set the (condition) of the for loop to go through the entire array. Can someone point out why this is?
Please Note: I understand this is not the only/or ideal way to remove elements from an array. This is simply some practice I am doing with for loops and Array methods.
my result looks like this after executing the code:
secondArray = 1,2,3,4,5,6,7,8,9,10
secondArray = 1,2,3,4,5
var secondArray = [1,2,3,4,5,6,7,8,9,10];
document.write("secondArray = " + secondArray)
for(i = 0; i < secondArray.length; i++){
secondArray.pop();
}
document.write("<br/>"+ "secondArray = " + secondArray)
Can someone please tell me why the length of secondArray
is 5 instead of 10?
The result I am looking for is for all elements to be popped off so the secondArray has an empty. However, it seems that only half of them are being popped off, even though I have set the (condition) of the for loop to go through the entire array. Can someone point out why this is?
Please Note: I understand this is not the only/or ideal way to remove elements from an array. This is simply some practice I am doing with for loops and Array methods.
my result looks like this after executing the code:
secondArray = 1,2,3,4,5,6,7,8,9,10
secondArray = 1,2,3,4,5
var secondArray = [1,2,3,4,5,6,7,8,9,10];
document.write("secondArray = " + secondArray)
for(i = 0; i < secondArray.length; i++){
secondArray.pop();
}
document.write("<br/>"+ "secondArray = " + secondArray)
Share
Improve this question
edited Aug 26, 2019 at 11:07
Vadim Kotov
8,2848 gold badges50 silver badges63 bronze badges
asked Jul 17, 2016 at 19:53
zzgooloozzgooloo
751 gold badge3 silver badges9 bronze badges
3
- 1 What are you expecting to happen? Because it's doing exactly what you're telling it to do. – Niet the Dark Absol Commented Jul 17, 2016 at 19:56
-
1
Every time you loop
secondArray.length
changes because you keep popping off elements – j08691 Commented Jul 17, 2016 at 19:57 -
If you want to empty out
secondArray
, then just dosecondArray.length = 0;
. – user663031 Commented Jul 17, 2016 at 20:10
5 Answers
Reset to default 4You should rewrite this as:
while (secondArray.length) secondArray.pop();
I will not address the question of why you are trying to empty an array by popping off all its elements, instead of just setting the length to 0.
secondArray.length
gets calculated for each iteration. On each iteration secondArray
has one item less.
To pop()
all the elements save secondArray.length
and use the variable:
var firstArray = [];
var secondArray = [1,2,3,4,5,6,7,8,9,10];
document.write("secondArray = " + secondArray)
var len = secondArray.length;
for(i = 0; i < len; i++){
secondArray.pop();
}
document.write("<br/>"+ "secondArray = " + secondArray)
Note
There are many other and better ways to empty an array as OP aware of already. The answer addresses the mon mistake of thinking that a for loop condition is calculated only at the first iteration.
var firstArray = [];
var secondArray = [1,2,3,4,5,6,7,8,9,10];
console.log(secondArray)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //nothing change
//first loop runs
for(i = 0; i < secondArray.length; i++){
// removing one element and now only 9 elements left
// means length have changed
// it's is doing his job as expacted
secondArray.pop();
console.log(secondArray)
}
// first run when length was 9 [1, 2, 3, 4, 5, 6, 7, 8, 9]
// second run when length was 8 [1, 2, 3, 4, 5, 6, 7, 8]
// third run when length was 7 [1, 2, 3, 4, 5, 6, 7]
// fourth run when length was 6 [1, 2, 3, 4, 5, 6]
// fifth run when length was 5 [1, 2, 3, 4, 5]
// and here loops end because of given condition
Because you are starting the loop at 0 and as long as the length of the array is less than your length the loop will continue. How every once it reaches 6 the i variable bee grater than the length of the array because you are popping a value out of the array with each run through the loop.
array = 10, 9, 8, 7, 6, 5, 4
var = 0, 1, 2, 3, 4, 5, 6
It terminates when your var[6] is greater than your array[4].
The pop method removes the last element from an array and returns that value to the caller.
It means the result is the expected.
If you want to clear the array put the initial array length in a var to make it static, but I can see two ways to do that better.
- Just assign array.length to 0
- Assign the array to be an empty array.
Check the docs
本文标签: javascriptThe pop() method using a for loopStack Overflow
版权声明:本文标题:javascript - The pop() method using a for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741403754a2376822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论