jQuery自定义数组操作类(类似于List集合的增删改查)

js外部文件,前提需要引入jquery类库。

封装类代码如下:

(function ($) {

    $.List = function () {

        var _list = new Array();
        //外部为数组赋值
        this.GetDataSource = function (arr) {
            if (IsArrayType(arr)) {
                _list = arr;
            } else {
                alert("指定元素非数组类型,赋值失败!");
            }
        };
        //添加一个元素
        this.Add = function (arg) {
            if (arg) {
                _list.push(arg);
            } else {
                alert("参数错误,添加元素失败!");
            }
            return _list;
        };
        //删除指定索引的元素
        this.RemoveAt = function (index) {             if (IsArrayIndex(index) && index < _list.length) {
                var i;
                var arr = new Array();
                for (i = 0; i < _list.length; i++) {
                    if (i != index) {
                        arr.push(_list[i]);
                    }
                }
                _list = arr;
                return _list;
            }
            else {
                alert("未获取到设置对象的实例,删除元素失败!");
            }
        };
        //按照指定的分割符显示出所有元素
        this.Split = function (arg) {
            arg = arg || ",";
            var i, res;
            res = "";
            if (_list.length > 0) {
                for (i = 0; i < _list.length; i++) {
                    res += _list[i].toString() + arg;
                }                 return res.substr(0, (res.length - arg.toString().length));
            } else {
                return "";
            }
        };
        //外部调用直接返回当前数组实力
        this.ToArray = function () {
            return _list;
        };
        //设置指定索引处的值为指定值
        this.Update = function (index, value) {
            if (IsArrayIndex(index) && index < _list.length) {
                _list[index] = value;
            }
            return _list;
        };
        //清空所有元素
        this.RemoveAll = function () {
            _list.splice(0, _list.length);
            return _list;
        };         //根据传入的值获取第一次出现在数组中的下标         this.IndexOf = function (value) {
            if (value) {
                var i;
                for (i = 0; i < _list.length; i++) {
                    if (_list[i] == value) {
                        return i;
                    }
                }
            }
            return -1;
        };
        //获取数组长度         this.Size = function () {
            return _list.length;
        };         //移除数组中重复的项         this.RemoveRepeat = function () {
            _list.sort();
            var rs = [];
            var cr = false;
            for (var i = 0; i < _list.length; i++) {
                if (!cr)
                    cr = _list[i];
                else if (cr == _list[i])
                    rs[rs.length] = i;
                else
                    cr = _list[i];
            }             for (var i = rs.length - 1; i >= 0; i--)
                this.RemoveAt(rs[i]);             return _list;
        };         //对数字数组元素排序,参数:0升序1降序         this.SortNumber = function (f) {
            if (!f) f = 0;
            if (f == 1) return _list.sort(function (a, b) { return b - a; });
            return _list.sort(function (a, b) { return a - b; });
        };         //私有方法
        //判断正确的数组下标
        function IsArrayIndex(index) {
            var reg = /^\d+$/;
            if (reg.test(index))
                return true;
            else
                return false;
        }
        //判断当前对象是否为数组对象
        function IsArrayType(arr) {
            if (typeof arr == 'object' && typeof arr.length == 'number')
                return true;
            else
                return false;
        }     }; //结束List的构造方法 })(jQuery);

页面调用时需要引入两个js文件:

<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="js/jquery.array.js" type="text/javascript"></script>
    <script type="text/javascript">         $(function () {
            var myList = new $.List();
            myList.Add(1);
            myList.Add("1906-07-08");
            myList.Add("hellow world");
            myList.RemoveAt(0);
            myList.Update(0, "11111111");
            //alert("数组被修改内容后的结果:" + myList.Split("|"));
            myList.RemoveAll();
            var arr = myList.ToArray();
            //alert("数组全部被删除后结果:" + arr);
            //alert("数组1当前长度:" + myList.Size());
            var myList2 = new $.List();
            myList2.Add(3);
            myList2.Add(1);
            myList2.Add(45);             myList2.Add(21);
            myList2.Add(-9);
            myList2.Add(1);             alert("第二个实例数组结果:" + myList2.ToArray());
            myList2.RemoveRepeat();
            alert("去重后第二个实例数组结果:" + myList2.ToArray());
            alert("去重后第二个实例数组长度:" + myList2.Size());
            myList2.SortNumber(1);
            alert("排序后的数组:" + myList2.ToArray());
            var arr3 = ["aaa", "bbb", "ccc", "ddd", "eee"];
            var arr4;
            myList2.GetDataSource(arr3);
            alert("重新赋值后结果:"+myList2.ToArray());         });
           </script>
</head>
上一篇:SQL Server 并发死锁解决案例备忘


下一篇:用jquery写自己的form表单验证