4 回答

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
delete
> myArray = ['a', 'b', 'c', 'd'] ["a", "b", "c", "d"]> delete myArray[0] true> myArray[0] undefined
undefined
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"]

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
Array.emove()方法
約翰·雷西格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 arrayarray.remove(1);// Remove the second-to-last item from the arrayarray.remove(-2); // Remove the second and third items from the arrayarray.remove(1,2); // Remove the last and second-to-last items from the arrayarray.remove(-2,-1);

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
myArray = ['a', 'b', 'c', 'd']; delete myArray[2];for (var count = 0; count < myArray.length; count++) { alert(myArray[count]);}
myArray = ['a', 'b', 'c', 'd']; myArray.splice(2,1);for (var count = 0; count < myArray.length; count++) { alert(myArray[count]);}

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
splice
delete
'c'
items
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"]
添加回答
舉報(bào)