日期:2014-05-16 浏览次数:20478 次
?
?
------------------------------------------------------删除数组元素------------------------------------------
?
1.根据下标删除数组元素
?
test =?newArray();
//define removeByIndex method as part of the array
Array.prototype.removeByIndex =?function(index) {
????this.splice(index, 1);
}
test[0] =?'Apple';
test[1] =?'Ball';
test[2] =?'Cat';
test[3] =?'Dog';
alert("Array before removing elements: "+test);
test.removeByIndex(2);
alert("Array after removing elements: "+test);
|
2.根据值删除数组元素
?
test =?newArray();
Array.prototype.removeByValue =?function(val) {
????for(vari=0; i<this.length; i++) {
????????if(this[i] == val) {
????????????this.splice(i, 1);
????????????break;
????????}
????}
}
test[0] =?'Apple';
test[1] =?'Ball';
test[2] =?'Cat';
test[3] =?'Dog';
alert("Array before removing elements: "+test);
test.removeByValue('Cat');
alert("Array after removing elements: "+test);
------------------------------------------------------------
|
http://falchion.iteye.com/blog/186656
删除数组?
方法一?