JS 数据元素删除:
// 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);
};
下面给出一些实际的用例:
// 移除数组中的第二项
array.remove(1);
// 移除数组中的倒数第二项
array.remove(-2);
// 移除数组中的第二项和第三项(从第二项开始,删除2个元素)
array.remove(1,2);
// 移除数组中的最后一项和倒数第二项(数组中的最后两项)
array.remove(-2,-1);
本文介绍了一种在JavaScript中删除数组元素的方法,并提供了具体的使用案例。该方法通过修改数组的length属性并结合Array.prototype.slice方法实现元素的移除。
452

被折叠的 条评论
为什么被折叠?



