日期:2014-05-16  浏览次数:20522 次

重写javascript的数组的方法
重写remove方法:
Array.prototype.indexOf = function(val) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] == val) return i;
            }
            return -1;
        };
        Array.prototype.remove = function(val) {
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };

function doVal(element) {
var gogalValue = $(element).parent().children(":last-child").val().split(",");
if($(element).attr("checked")==false) {
gogalValue.remove($(element).val());
} else {
gogalValue.push($(element).val());
}
$(element).parent().children(":last-child").val(gogalValue.toString());
}
重写contains方法:
Array.prototype.contains = function (element) {
for (var i = 0; i < this.length; i++) {
if (this[i] == element) {
return true;
}
}
return false;
};
//数组排序
function compareInt(obj1, obj2){
    var iNum1 = parseInt(obj1.id);//强制转换成int 型;
    var iNum2 = parseInt(obj2.id);
    if(iNum1 < iNum2){
        return -1;
    }else if(iNum1 > iNum2){
        return 1;
    }else{
        return 0;
    }
}
specArr.sort(compareInt);//重新排序;