获取JS数组中所有重复元素

//获取数组内所有重复元素,并以数组返回

//例:入参数组['1','2','4','7','1','2','2'] 返回数组:['1','2']

function GetRepeatFwxmmc(ary1){

  var ary = ary1.sort();//数组排序

  var cffwxmsAry = new Array();

  //所有重复元素添加进新数组内

  for(var i=0;i<ary.length;i++){

    if (ary[i]==ary[i+1]){

      cffwxmsAry.push(ary[i]);

    }

  }

  var result = [], isRepeated;

  //对重复元素数组进行元素去重

  for (var k = 0; k < cffwxmsAry.length; k++) {

    isRepeated = false;

    for (var j = 0;j < result.length; j++) {

      if (cffwxmsAry[k] == result[j]) {

        isRepeated = true; break;

      }

    }

    if (!isRepeated) {

      result.push(cffwxmsAry[k]);

    }

  }

  return result;

}

posted @ 2017-07-04 14:27  其善-从之  阅读(9885)  评论(0编辑  收藏  举报