delete
delete
will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined
:
1 | // Array |
但是当delete
Object的property时候就没有这个问题:
1 | // Object |
Array.splice
myArray.splice(start, deleteCount)
actually removes the element, reindexes the array, and changes its length.
实际上删除元素,重新索引数组,并更改其长度.
1 | > myArray = ['a', 'b', 'c', 'd'] |
Array.remove of jQuery
John Resig, creator of jQuery created a very handy Array.remove method that I always use it in my projects.
1 | // Array Remove - By John Resig (MIT Licensed) |
and here’s some examples of how it could be used:
1 | // Remove the second item from the array |
参考文章
- MDN
delete
- MDN Array.splice
- What is the difference between using the delete operator on the array element as opposed to using the Array.splice method?