새로운 것과 오래된 두 개의 그룹을 추가하고 삭제할 수 있는 JS 코드 비교

2662 단어
 
  
   , 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]);
                }
            }
        }
        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]);
        //        }
        //    }
        //}
        //alert("dels:" + dels); //
    }

호출 방식: var arr = [1,2,2,3,3,4,5];
              var arr2=[1,2,5]
       var addStr=Return_AddStrFn(arr2,arr);

좋은 웹페이지 즐겨찾기