在数组元素上使用delete运算符与使用array.splice方法有什么区别?
例如:
myArray = ['a', 'b', 'c', 'd'];
delete myArray[1];
// or
myArray.splice (1, 1);
如果我可以像删除对象那样删除数组元素,为什么还要使用拼接方法?
在数组元素上使用delete运算符与使用array.splice方法有什么区别?
例如:
myArray = ['a', 'b', 'c', 'd'];
delete myArray[1];
// or
myArray.splice (1, 1);
如果我可以像删除对象那样删除数组元素,为什么还要使用拼接方法?
当前回答
它们是不同的东西,有不同的目的。
splice是特定于数组的,当用于删除时,会从数组中删除条目,并向上移动之前的所有条目以填补空白。(它也可以用于插入条目,或同时插入两个条目。)拼接将更改数组的长度(假设它不是无操作调用:array.splice(x,0))。
delete不是特定于数组的;它设计用于对象:它从您使用它的对象中删除属性(键/值对)。它只适用于数组,因为JavaScript中的标准(例如,非类型化)数组根本不是数组*,它们是对某些财产进行特殊处理的对象,例如名称为“数组索引”(定义为字符串名称“…其数值i在+0≤i<2^32-1”的范围内)和长度的索引。当您使用delete删除数组条目时,它所做的就是删除该条目;它不会移动后面的其他条目来填补空白,因此数组变得“稀疏”(有些条目完全缺失)。它对长度没有影响。
目前对这个问题的一些回答错误地指出,使用delete“将条目设置为undefined”。这不正确。它会完全删除条目(属性),留下一个间隙。
让我们用一些代码来说明差异:
console.log(“使用“拼接”:”);var a=[“a”,“b”,“c”,“d”,“e”];console.log(a.length);//5.a.拼接(0,1);console.log(a.length);//4.console.log(a[0]);//“b”
console.log(“使用`delete`”);var a=[“a”,“b”,“c”,“d”,“e”];console.log(a.length);//5.删除[0];console.log(a.length);//静止5console.log(a[0]);//未定义console.log(a中的“0”);//假的console.log(a.hasOwnProperty(0));//假的
console.log(“设置为`undefined`”);var a=[“a”,“b”,“c”,“d”,“e”];console.log(a.length);//5.a[0]=未定义;console.log(a.length);//静止5console.log(a[0]);//未定义console.log(a中的“0”);//真的console.log(a.hasOwnProperty(0));//真的
*(这是我贫血的小博客上的一篇文章)
其他回答
我在试图理解如何从数组中删除每一个元素时偶然发现了这个问题。这里是拼接和删除的比较,用于从项数组中删除每个“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"]
其他人已经正确比较了删除和拼接。
另一个有趣的比较是delete与undefined:一个被删除的数组项比一个刚刚设置为undefineed的数组项使用更少的内存;
例如,此代码不会完成:
let y = 1;
let ary = [];
console.log("Fatal Error Coming Soon");
while (y < 4294967295)
{
ary.push(y);
ary[y] = undefined;
y += 1;
}
console(ary.length);
它会产生以下错误:
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory.
所以,正如您所看到的,undefined实际上占用了堆内存。
但是,如果您还删除了ary项(而不是将其设置为undefined),代码将缓慢完成:
let x = 1;
let ary = [];
console.log("This will take a while, but it will eventually finish successfully.");
while (x < 4294967295)
{
ary.push(x);
ary[x] = undefined;
delete ary[x];
x += 1;
}
console.log(`Success, array-length: ${ary.length}.`);
这些都是极端的例子,但它们表明了我在任何地方都没有看到有人提到的删除。
function remove_array_value(array, value) {
var index = array.indexOf(value);
if (index >= 0) {
array.splice(index, 1);
reindex_array(array);
}
}
function reindex_array(array) {
var result = [];
for (var key in array) {
result.push(array[key]);
}
return result;
}
例子:
var example_arr = ['apple', 'banana', 'lemon']; // length = 3
remove_array_value(example_arr, 'banana');
香蕉被删除,数组长度=2
为什么不只是过滤?我认为这是考虑js中数组的最清楚的方法。
myArray = myArray.filter(function(item){
return item.anProperty != whoShouldBeDeleted
});
因为delete只从数组中的元素中删除对象,所以数组的长度不会改变。拼接将删除对象并缩短阵列。
以下代码将显示“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]);
}