javascript不会将数组的所有元素拼接在一起

时间:2022-03-23 21:26:27

http://jsfiddle.net/4wKuj/8/

http://jsfiddle.net/4wKuj/8/

  var myarr=new Array("Saab","Volvo","BMW");       
  console.log(myarr);
  for (var i=0; i<myarr.length; i++) {
    myarr.splice(i,1);
  }
  console.log(myarr);

Note: in the real implementation I am not trying to empty the array so plz do not suggest other way of emptying an array.

注意:在实际的实现中,我并没有尝试清空数组,所以plz不建议用其他方式清空数组。

Why I still see "volvo" in the console log ?

为什么我仍然在控制台日志中看到“volvo”?

Should not it be removed either , same as other ?

难道它也不应该被移除,和其他的一样吗?

Thank You

谢谢你!

4 个解决方案

#1


5  

What it does :

它所做的:

  • first iteration, i=0, removes "Saab", => array is ["Volvo","BMW"]
  • 第一次迭代,i=0,移除“Saab”,=>阵列为["Volvo","BMW"]
  • second iteration, i=1, removes "BMW" because this is what you have at index 1
  • 第二次迭代,i=1,移除“BMW”,因为这是索引1中所拥有的。

#2


1  

After the first splice:

后第一个接头:

i = 1
myarr = ["Volvo", "BMW"]

So it will remove "BMW". After the second splice:

因此,它将移除“宝马”。后第二个接头:

i = 2
myarr = ["Volvo"]

And the loop will not continue. Better do it like this:

循环不会继续。最好这样做:

while (myarr.length > 1) {
    myarr.splice(0, 1);
}

#3


0  

before loop, your array looks like

在循环之前,您的数组看起来是这样的。

["Saab","Volvo","BMW"]

["萨博”、“沃尔沃”、“宝马”)

after first iteration of loop you delete first element of array, and now array looks like

在第一次循环迭代之后,删除数组的第一个元素,现在数组看起来是这样的

["Volvo","BMW"]

(“沃尔沃”、“宝马”)

during second iteration your "i" variable has '1' (one) value that corresponds to second element of array, in other words you say:

在第二次迭代中,“i”变量的值为“1”(1),对应于数组的第二个元素,换句话说:

delete in ["Volvo","BMW"] array element with index '1'

删除["Volvo","BMW"]数组元素索引" 1 "

#4


0  

If you remove an item from the array, you also need to adjust the index value, f.ex:

如果从数组中删除一个项,还需要调整索引值f.ex:

var myarr=new Array("Saab","Volvo","BMW");       
for (var i=0; i<myarr.length; i++) {
  myarr.splice(i--,1);
}

Tip: If you are using a loop remove items from an array based on it’s value, you can use other methods, f.ex Array.filter:

提示:如果使用循环根据数组的值从数组中删除项,可以使用其他方法f。例Array.filter:

myarr = myarr.filter(function(val) {
    return val !== 'Volvo'
});

Array.indexOf also comes to mind.

数组中。我也想到了indexOf。

#1


5  

What it does :

它所做的:

  • first iteration, i=0, removes "Saab", => array is ["Volvo","BMW"]
  • 第一次迭代,i=0,移除“Saab”,=>阵列为["Volvo","BMW"]
  • second iteration, i=1, removes "BMW" because this is what you have at index 1
  • 第二次迭代,i=1,移除“BMW”,因为这是索引1中所拥有的。

#2


1  

After the first splice:

后第一个接头:

i = 1
myarr = ["Volvo", "BMW"]

So it will remove "BMW". After the second splice:

因此,它将移除“宝马”。后第二个接头:

i = 2
myarr = ["Volvo"]

And the loop will not continue. Better do it like this:

循环不会继续。最好这样做:

while (myarr.length > 1) {
    myarr.splice(0, 1);
}

#3


0  

before loop, your array looks like

在循环之前,您的数组看起来是这样的。

["Saab","Volvo","BMW"]

["萨博”、“沃尔沃”、“宝马”)

after first iteration of loop you delete first element of array, and now array looks like

在第一次循环迭代之后,删除数组的第一个元素,现在数组看起来是这样的

["Volvo","BMW"]

(“沃尔沃”、“宝马”)

during second iteration your "i" variable has '1' (one) value that corresponds to second element of array, in other words you say:

在第二次迭代中,“i”变量的值为“1”(1),对应于数组的第二个元素,换句话说:

delete in ["Volvo","BMW"] array element with index '1'

删除["Volvo","BMW"]数组元素索引" 1 "

#4


0  

If you remove an item from the array, you also need to adjust the index value, f.ex:

如果从数组中删除一个项,还需要调整索引值f.ex:

var myarr=new Array("Saab","Volvo","BMW");       
for (var i=0; i<myarr.length; i++) {
  myarr.splice(i--,1);
}

Tip: If you are using a loop remove items from an array based on it’s value, you can use other methods, f.ex Array.filter:

提示:如果使用循环根据数组的值从数组中删除项,可以使用其他方法f。例Array.filter:

myarr = myarr.filter(function(val) {
    return val !== 'Volvo'
});

Array.indexOf also comes to mind.

数组中。我也想到了indexOf。