比较新旧两个数组值得增加和删除

  以前项目中用到自己写的,应该没有bug吧,有更好的也欢迎大家指教,
 <script type="text/javascript">
      //  以前项目中用到自己写的,应该没有bug吧,有更好的也欢迎大家指教,
        var Return_AddStrFn = function (oldArr, newArr) {
            var t = this;
            //去重复的方法
            Array.prototype.unique4 = function () {
                // this = arr;
                var temp = new Array();
                this.sort();
                for (i = 0; i < this.length; i++) {
                    if (this[i] == this[i + 1]) {
                        continue;
                    }
                    temp[temp.length] = this[i];
                }
                return temp;
            }
            var a = d = oldArr.unique4(); //旧数组
            var b = e = newArr.unique4();//新数组
            var c = [];
            var dels = [];
            function f() {
                a.sort();
                b.sort();
                var i = 0;
                var j = 0;
                while (i < a.length && j < b.length) {
                    if (a[i] < b[j]) {
                        c.push(a[i]);
                        i++;
                    } else if (b[j] < a[i]) {
                        c.push(b[j]);
                        j++;
                    } else {
                        i++;
                        j++;
                    }
                }
                while (i < a.length) {
                    c.push(a[i]);
                    i++;
                }
                while (j < b.length) {
                    c.push(b[j]);
                    j++;
                }
            }
            f();
            //alert("c:" + c);
            //alert("d:" + d);
            var addstr = [];
            for (var i = 0; i < c.length; i++) {
                for (var j = 0; j < e.length; j++) {
                    if (e[j] == c[i]) {
                        addstr.push(e[j]);
                    }
                }
            }
            var difArr = {}
            difArr["addstr"] = addstr;
          
          //  return addstr; //新增的
            for (var i = 0; i < c.length; i++) {
                for (var j = 0; j < d.length; j++) {
                    if (d[j] == c[i]) {
                        dels.push(d[j]);
                    }
                }
            }   
            difArr["dels"]=dels;
            return difArr;
            //alert("dels:" + dels); //删除的
        }
        var arr = ['2000_1', '2000_2', '2000_3', '2000_4', '2000_5', '2000_6', '2000_7','2001_8']; //就得
        var arr2 = ['2000_1', '2000_2', '2000_3', '2000_4', '2000_5', '2000_6', '2000_8']//新的
        var data = Return_AddStrFn(arr, arr2);
        var addStr = data.addstr;
        var delStr = data.dels;
        alert("addStr:" + addStr);
        alert("delStr:" + delStr);
    </script>

posted on 2013-10-29 15:30  _o~ 努力!  阅读(824)  评论(2编辑  收藏  举报

导航