I have an array:
我有一个数组:
data.Dealer.car[0]
data.Dealer.car[1]
data.Dealer.car[2]
If I do this:
如果我这样做:
alert(data.Dealer.car.length);
delete data.Dealer.car[1];
alert(data.Dealer.car.length);
It gives me the same count each time. Does the removed element still exist?
它每次给我相同的数量。删除的元素是否仍然存在?
6 个解决方案
#1
JavaScript arrays aren't sparse, if you have a 0 and a 2, then element 1 must exist. Meaning the length
is going to be 3.
JavaScript数组不稀疏,如果您有0和2,则元素1必须存在。意思是长度将是3。
#2
If you want to remove an item, use the splice
method:
如果要删除项目,请使用拼接方法:
alert(data.Dealer.car.length);
data.Dealer.car.splice(1, 1);
alert(data.Dealer.car.length);
But notice that the indices have changed.
但请注意,指数已发生变化。
#3
Array.shift() would remove the first item from the array and make it shorter. Array.pop() will remove the last item.
Array.shift()将从数组中删除第一个项目并使其更短。 Array.pop()将删除最后一项。
#4
Building on what @Ray Hidayat posted:
以@Ray Hidayat发布的内容为基础:
JavaScript arrays are sparse. If you have an array of length 3, deleting the reference at [1] will simply "unset" that item, not remove it from the array, or update that array's length. You could accomplish the same with
JavaScript数组很稀疏。如果你有一个长度为3的数组,删除[1]处的引用将只是“取消设置”该项,而不是从数组中删除它,或更新该数组的长度。你可以完成同样的事情
myArray = [0, , 2, , , , ]; // length 6; last three items undefined
Here's a Fiddle: http://jsfiddle.net/WYKDz/
这是一个小提琴:http://jsfiddle.net/WYKDz/
NOTE: removing items from the middle of large arrays can be computationally intensive. See this post for more info: Fastest way to delete one entry from the middle of Array()
注意:从大型数组中间删除项目可能是计算密集型的。有关更多信息,请参阅此文章:从Array中间删除一个条目的最快方法()
#5
I think you're looking for this:
我想你正在寻找这个:
var arr = [0,1,2,3,4];
alert( arr.splice( 2, 1 ) ); // alerts 2, the element you're removing
alert( arr ) // alerts 0,1,3,4 - the remaining elements
here's a MDC reference
这是MDC参考
#6
Ok.. fixed this now as the array was still allocated.
好了..现在修复了这个,因为数组仍然被分配。
I needed to basically do:
我基本上需要做:
var newCar = new Array();
for (i = 0 ; i < tblSize -2; i ++)
{
newCar[i]=data.Dealer.car[i];
}
data.Dealer.car = newCar;
#1
JavaScript arrays aren't sparse, if you have a 0 and a 2, then element 1 must exist. Meaning the length
is going to be 3.
JavaScript数组不稀疏,如果您有0和2,则元素1必须存在。意思是长度将是3。
#2
If you want to remove an item, use the splice
method:
如果要删除项目,请使用拼接方法:
alert(data.Dealer.car.length);
data.Dealer.car.splice(1, 1);
alert(data.Dealer.car.length);
But notice that the indices have changed.
但请注意,指数已发生变化。
#3
Array.shift() would remove the first item from the array and make it shorter. Array.pop() will remove the last item.
Array.shift()将从数组中删除第一个项目并使其更短。 Array.pop()将删除最后一项。
#4
Building on what @Ray Hidayat posted:
以@Ray Hidayat发布的内容为基础:
JavaScript arrays are sparse. If you have an array of length 3, deleting the reference at [1] will simply "unset" that item, not remove it from the array, or update that array's length. You could accomplish the same with
JavaScript数组很稀疏。如果你有一个长度为3的数组,删除[1]处的引用将只是“取消设置”该项,而不是从数组中删除它,或更新该数组的长度。你可以完成同样的事情
myArray = [0, , 2, , , , ]; // length 6; last three items undefined
Here's a Fiddle: http://jsfiddle.net/WYKDz/
这是一个小提琴:http://jsfiddle.net/WYKDz/
NOTE: removing items from the middle of large arrays can be computationally intensive. See this post for more info: Fastest way to delete one entry from the middle of Array()
注意:从大型数组中间删除项目可能是计算密集型的。有关更多信息,请参阅此文章:从Array中间删除一个条目的最快方法()
#5
I think you're looking for this:
我想你正在寻找这个:
var arr = [0,1,2,3,4];
alert( arr.splice( 2, 1 ) ); // alerts 2, the element you're removing
alert( arr ) // alerts 0,1,3,4 - the remaining elements
here's a MDC reference
这是MDC参考
#6
Ok.. fixed this now as the array was still allocated.
好了..现在修复了这个,因为数组仍然被分配。
I needed to basically do:
我基本上需要做:
var newCar = new Array();
for (i = 0 ; i < tblSize -2; i ++)
{
newCar[i]=data.Dealer.car[i];
}
data.Dealer.car = newCar;