美文网首页
javaScript中处理数组中重复元素的办法

javaScript中处理数组中重复元素的办法

作者: 90后的晨仔 | 来源:发表于2017-07-11 23:20 被阅读15次
    参考链接:http://blog.csdn.net/zhihua_w/article/details/52184289
    

       以下方法自己都已经进行简单测试并且通过,需要的朋友可以直接拷贝使用!🙂

       一、获取数组中相同元素的下标

    /**
         * @param    needle       重复元素值
         * @param    elementArray   对应的元素数组
         * @returns {Array}  返回一个数组
         */
      var  testArr = ["1","6","3","2","4","1","3","2","3","4","5"];
     function searchKeys(needle, elementArray)
        {
            console.log("needle=>"+needle,"elementArray=>"+elementArray)
            var result = [];
            for (i in elementArray)
            {
                if (elementArray[i] == needle)
                {
                    result.push(i);
                }
            }
            return result;
        }
    
        console.log(searchKeys("1",testArr));
    

       二、将数组中重复元素过滤掉重新返回一个新的数组

    1、方法一:
    var  testArr = ["1","6","3","2","4","1","3","2","3","4","5"];
        Array.prototype.method1 = function(){
            var arr = [];    //定义一个临时数组
            for(var i = 0; i < this.length; i++){    //循环遍历当前数组
                //判断当前数组下标为i的元素是否已经保存到临时数组
                //如果已保存,则跳过,否则将此元素保存到临时数组中
                if(arr.indexOf(this[i]) == -1){
                    arr.push(this[i]);
                }
            }
            return arr;
        }
    
        document.write(testArr.method1())//这个调用方法,将结果显示出来
    
    2、方法二:
       var  testArr = ["1","6","3","2","4","1","3","2","3","4","5"];
    
        Array.prototype.method2 = function(){
            var h = {};    //定义一个hash表
            var arr = [];  //定义一个临时数组
    
            for(var i = 0; i < this.length; i++){    //循环遍历当前数组
                //对元素进行判断,看是否已经存在表中,如果存在则跳过,否则存入临时数组
                if(!h[this[i]]){
                    //存入hash表
                    h[this[i]] = true;
                    //把当前数组元素存入到临时数组中
                    arr.push(this[i]);
                }
            }
            return arr;
        }
        document.write(testArr.method2())
    
    3、方法三:
       var  testArr = ["1","6","3","2","4","1","3","2","3","4","5"];
    
        Array.prototype.method3 = function(){
            //直接定义结果数组
            var arr = [this[0]];
            for(var i = 1; i < this.length; i++){    //从数组第二项开始循环遍历此数组
                //对元素进行判断:
                //如果数组当前元素在此数组中第一次出现的位置不是i
                //那么我们可以判断第i项元素是重复的,否则直接存入结果数组
                if(this.indexOf(this[i]) == i){
                    arr.push(this[i]);
                }
            }
            return arr;
    
        }
    
        document.write(testArr.method3())
    
    
    3、方法四:将结果按照从小到大的顺序排列出来
      var  testArr = ["1","6","3","2","4","1","3","2","3","4","5"];
    
        Array.prototype.method4 = function() {
            //将数组进行排序
            this.sort();
            //定义结果数组
            var arr = [this[0]];
            for (var i = 1; i < this.length; i++) {    //从数组第二项开始循环遍历数组
                //判断相邻两个元素是否相等,如果相等说明数据重复,否则将元素写入结果数组
                if (this[i] !== arr[arr.length - 1]) {
                    arr.push(this[i]);
                }
            }
            return arr;
    
        }
            document.write(testArr.method4());
    

    相关文章

      网友评论

          本文标题:javaScript中处理数组中重复元素的办法

          本文链接:https://www.haomeiwen.com/subject/ptlvhxtx.html