使用delete
操作符和使用Array.splice
方法在数组元素上的区别是什么?
例如:
myArray = ['a', 'b', 'c', 'd'];
delete myArray[1];
// or
myArray.splice (1, 1);
如果我可以像操作对象一样删除数组元素,那为什么还需要splice方法呢?
使用delete
操作符和使用Array.splice
方法在数组元素上的区别是什么?
例如:
myArray = ['a', 'b', 'c', 'd'];
delete myArray[1];
// or
myArray.splice (1, 1);
如果我可以像操作对象一样删除数组元素,那为什么还需要splice方法呢?
delete
会删除对象的属性,但不会重新索引数组或更新其长度。这使它看起来好像是未定义的:
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
请注意,实际上它并没有设置为值undefined
,而是从数组中删除该属性,使其看起来像是未定义。Chrome开发工具通过在记录数组时打印empty
来明确这种差异。> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]
myArray.splice(start, deleteCount)
实际上是移除元素,重新索引数组,并更改其长度。
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
delete
确实 移除了该元素,但不会重新索引数组或更新其长度(因为由前者已经隐含了长度始终是最高索引+1)。 - Felix Klingdelete myArray[0]
这种语法,会提示"Expected an operator and instead saw 'delete'."建议使用splice
代替。 - esengineerdelete myArray[0]
and myArray[0] = undefined
.在前一种情况下,该属性被完全从数组对象中删除。在第二种情况下,该属性仍然存在,其值为“undefined”。" - T.J. CrowderjQuery 的创始人 John Resig 创建了一个非常方便的 Array.remove
方法,我在自己的项目中经常使用它。
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
以下是一些使用示例:
// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);
var rest = this.slice(parseInt(to || from) + 1 || this.length);
。 - Richard Inglisthis.push.apply(this, rest)
,然后返回它返回的值。 - billyfor(var i in array)
循环迭代数组时会产生不必要的副作用。我已经将其删除并改用splice。 - alexykot因为delete只是从数组中移除对象,所以数组的长度不会改变。而splice则移除对象并缩短数组。
以下代码将显示 "a", "b", "undefined", "d"
myArray = ['a', 'b', 'c', 'd']; delete myArray[2];
for (var count = 0; count < myArray.length; count++) {
alert(myArray[count]);
}
而这将显示 "a","b","d"
myArray = ['a', 'b', 'c', 'd']; myArray.splice(2,1);
for (var count = 0; count < myArray.length; count++) {
alert(myArray[count]);
}
myArray.splice(2,1)
。 - iancoleman在尝试理解如何从数组中删除每个元素的情况下,我偶然发现了这个问题。这里进行比较使用splice
和delete
从items
数组中删除每个'c'
。
var items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];
while (items.indexOf('c') !== -1) {
items.splice(items.indexOf('c'), 1);
}
console.log(items); // ["a", "b", "d", "a", "b", "d"]
items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];
while (items.indexOf('c') !== -1) {
delete items[items.indexOf('c')];
}
console.log(items); // ["a", "b", undefined, "d", "a", "b", undefined, "d"]
来自 JavaScript 核心参考文档 1.5 > 操作符 > 特殊操作符 > delete 操作符:
当你删除数组元素时,数组的长度不会受到影响。例如,如果你删除 a[3],a[4]仍然是 a[4],而a[3]则变为undefined。即使你删除了数组的最后一个元素 (delete a[a.length-1]),这个规则也同样适用。
如上所述,使用 splice()
方法似乎非常适合。 Mozilla 文档:
splice()
方法通过删除现有元素和/或添加新元素来更改数组的内容。
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
myFish.splice(2, 0, 'drum');
// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
myFish.splice(2, 1);
// myFish is ["angel", "clown", "mandarin", "sturgeon"]
(需要翻译的内容在这里)
array.splice(start)
array.splice(start, deleteCount)
array.splice(start, deleteCount, item1, item2, ...)
开始修改数组的索引值。如果大于数组长度,实际的起始索引将被设置为数组长度。如果为负数,则从末尾开始那么多个元素。
一个整数,表示要删除的旧数组元素数量。如果deleteCount为0,则不会删除任何元素,此时您应该至少指定一个新元素。如果deleteCount大于从start开始的数组剩余元素数量,则将删除数组到末尾的所有元素。
如果省略deleteCount,则deleteCount将等于(arr.length - start)
。
要添加到数组中的元素,从起始索引开始。如果未指定任何元素,splice()
将仅从数组中删除元素。
一个包含已删除元素的数组。如果只删除了一个元素,则返回一个长度为1的数组。如果没有删除任何元素,则返回一个空数组。
[...]
splice
可以使用数字索引。
而 delete
可以用于其他类型的索引。
例子:
delete myArray['text1'];
arr[2]
是数组 arr
的第三个元素,而 arr['x']
是对象的属性 x
。在实际使用中并没有太大区别,但它们在内部存储方式上是不同的。 - David R Tribblevar arr = [1,2,3,4]; delete arr[2]; //result [1, 2, 3:, 4]
console.log(arr)
var arr = [1,2,3,4]; arr.splice(1,1); //result [1, 3, 4]
console.log(arr);
delete myObj.propName; // , or:
delete myObj["propName"]; // Equivalent.
var arr = [1,2,3,4,5];
delete arr[3];
for (var i = 0; i <= arr.length; i++)
console.log(arr[i]);
将展示出意想不到的结果。 - Kristian Williams使用delete
运算符和splice()
方法后,可以通过记录每个数组的长度来看到它们之间的区别。例如:
var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
delete trees[3];
console.log(trees); // ["redwood", "bay", "cedar", empty, "maple"]
console.log(trees.length); // 5
delete
操作符会从数组中删除元素,但是该元素的“占位符”仍然存在。尽管oak
已经被删除,但它仍然占据着数组中的空间。由于这个原因,数组的长度仍然为5。
var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
trees.splice(3,1);
console.log(trees); // ["redwood", "bay", "cedar", "maple"]
console.log(trees.length); // 4
< p > splice()
方法完全删除目标值和"占位符"。 oak
已被删除,以及它在数组中所占据的空间。数组的长度现在为 4。
.splice
,请参考这个问题:在JavaScript中从数组中删除元素。 - Rob Wdelete
和Array.prototype.splice
之间的区别。 - chharvey