zoukankan      html  css  js  c++  java
  • 比较新旧两个数组值得增加和删除

      以前项目中用到自己写的,应该没有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>
  • 相关阅读:
    小笔记系列——Excel中获取当前日期
    Git 错误:OpenSSL SSL_read: Connection was reset, errno 10054
    cmd_切换文件目录的几种方法
    Jupyter Notebook 常用操作(持续更新中……)
    chrome 浏览器书签保存
    各种开发工具注释的快捷键(持续更新中…)
    Spyder 快捷键(注释、跳转、缩进)
    ISlide插件安装后,PPT无法正常关闭
    [TimLinux] 操作系统实战45讲
    [TimLinux] vnc and go bashrc
  • 原文地址:https://www.cnblogs.com/rc727512646/p/3394415.html
Copyright © 2011-2022 走看看