Comparing the old and new two arrays is worth adding and removing JS code

  • 2020-03-26 21:41:00
  • OfStack


 I used to write it myself in previous projects, but I don't think so bug Well, any better ones are welcome, 
var Return_AddStrFn=function (oldArr, newArr) {
        var t = this;
       //To repeat the method
        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(); //The old array
        var b = e = newArr.unique4();//The new array
        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; //The addition of
        //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); // Delete the 
    }

Var arr = [1, 2,2,3, 3, 4, 5];
                          Var arr2 =,2,5 [1]

            Var addStr = Return_AddStrFn (arr2, arr);


Related articles: