今天遇到一个数组的问题,后来发现在javascript里面没有remove这个方法,在网上找了各种方法进行解决,现在来分享一下自己做的过程:
方法1:
//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);
};
这个函数扩展了JavaScript的内置对象Array,这样,我们以后的所有声明的数组都会自动的拥有remove能力,
function police(obj,_id,sort){//传递过来的this,id号码,序号--控制数字下标
tempArray[sort]=id;
}else{
tempArray.remove(sort);
}
alert(tempArray[i]);
}
}
后来发现这种方法不行,因为序号传递过来,数组的空间已经划分了,除非你是按照顺序一个个勾选,这样才不会出现下面遍历数组的时候出现未定义的情况。
方法2:js删除指定元素值进行封装
Array.prototype.indexOf = function(val) {
function police(obj,_id){//传递过来的this,id号码
tempArray.push(id);
}else{
tempArray.remove(_id);
}
alert(tempArray[i]);
}
}
问题解决