在数组元素上使用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);
如果我可以像删除对象那样删除数组元素,为什么还要使用拼接方法?
当前回答
你可以用这样的东西
var my_array=[1,2,3,4,5,6];删除my_array[4];console.log(my_array.filter(函数(a){return typeof a!=='undefined';}));//[1,2,3,4,6]
其他回答
delete将删除对象属性,但不会重新索引数组或更新其长度。这使得它看起来像是未定义的:
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray[0]
undefined
请注意,它实际上没有设置为值undefined,而是从数组中删除了属性,使其看起来未定义。Chrome开发工具在记录阵列时打印为空,从而明确了这一区别。
> myArray[0]
undefined
> myArray
[empty, "b", "c", "d"]
splice(start,deleteCount)实际上删除了元素,重新索引数组,并更改其长度。
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
如上多次所述,使用夹板()看起来非常合适。Mozilla文档:
splice()方法通过删除现有元素和/或添加新元素来更改数组的内容。var myFish=['angel','小丑','普通话','鲟鱼'];myFish.splice(2,0,'鼓');//myFish是[“天使”、“小丑”、“鼓”、“普通话”、“鲟鱼”]myFish.splice(2,1);//myFish是[“天使”、“小丑”、“普通话”、“鲟鱼”]语法array.拼接(开始)array.spling(开始,删除计数)array.spling(start,deleteCount,item1,item2,…)参数开始开始更改阵列的索引。如果大于数组的长度,则实际起始索引将设置为数组的长度。如果是否定的,将从结尾开始许多元素。删除计数一个整数,指示要删除的旧数组元素的数量。如果deleteCount为0,则不会删除任何元素。在这种情况下,应至少指定一个新元素。如果deleteCount大于数组中从开始时剩余的元素数,则将删除数组末尾的所有元素。如果省略deleteCount,deleteCount将等于(arr.length-start)。项目1,项目2。。。要添加到数组的元素,从开始索引开始。如果不指定任何元素,splice()将只从数组中删除元素。返回值包含已删除元素的数组。如果只删除一个元素,则返回一个元素的数组。如果没有删除任何元素,则返回一个空数组。[...]
Array.remove()方法
jQuery的创建者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);
John的网站
它们是不同的东西,有不同的目的。
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));//真的
*(这是我贫血的小博客上的一篇文章)
从核心JavaScript 1.5参考>运算符>特殊运算符>删除运算符:
删除数组元素时数组长度不受影响。对于例如,如果删除[3],则[4]为仍然a[4]和a[3]未定义。这即使删除最后一个数组的元素(删除a[a.length-1])。