怎么获取重复的数据

数据是这样的

var  node=[
            {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
            {
                "listId":2,
                "projectName":"公司",
                "projectVersion":"1.1.2.0"
            },
            {
                "listId":3,
                "projectName":"学校",
                "projectVersion":"2.1.1.0"
            },
            {
                "listId":4,
                "projectName":"家",
                "projectVersion":"3.1.2.0"
            },
            {
                "listId":5,
                "projectName":"朋友",
                "projectVersion":"4.1.2.0"
            },
            {
                "listId":6,
                "projectName":"啦啦啦",
                "projectVersion":"5.1.2.0"
            },
            {
                "listId":7,
                "projectName":"哈哈哈",
                "projectVersion":"6.1.2.0"
            },
            {
                "listId":8,
                "projectName":"嘻嘻嘻",
                "projectVersion":"7.1.2.0"
            }
        ];

需要最后得到新的数据

[
            {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            },
             {
                "listId":10,
                "projectName":"中行",
                "projectVersion":"0.1.2.0"
            }]
回复
阅读 3k
4 个回答

我是先计数,然后再处理
//计数处理

            let opt={};
            let namePP="";
            node.forEach(function(el,index){
              if(!opt[el.projectName]){
                opt[el.projectName] = 1;
            }else{
                opt[el.projectName] += 1;
            }
            if(opt[el.projectName]>1){
                namePP = el.projectName
             }
           })
            let optionsarr=node.filter(function(item){ if(item.projectName == namePP){ return true}})
function getHashCode(str) {
    var hash = 0, i, chr, len;
    if (str.length === 0) return hash;
    for (i = 0, len = str.length; i < len; i++) {
        chr = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
}

function getReapeat(node) {
    var ret = [];
    var map = {};
    node.forEach(function (item) {
        var hashCode = getHashCode(JSON.stringify(item));
        if (map[hashCode]) {
            map[hashCode].push(item)
        } else {
            map[hashCode] = [item]
        }
    })
    for (var key in map) {
        //node中可能多个不止一个元素重复多次
        if (map[key].length > 1) {
            ret.push(map[key])
        }
    }
    return ret;
}
//这里得到是个数组,此例子中如果要得到正确的结果=reapeatList[0]
var reapeatList = getReapeat(node)

假设原有的node中有两个

 {
    "listId":8,
    "projectName":"嘻嘻嘻",
    "projectVersion":"7.1.2.0"
 }

那么reapeatList 结果如下图
图片描述

function searchRepeat(node,type){ // 提取重复项
var arr = []
for (var i = 0, l = node.length; i < l; i++) {

for (var j = i + 1; j < l; j++) {
  if (node[i][type] == node[j][type])
  arr.push(node[j])
}

}
return arr
}
searchRepeat(node,'listId') // 提取字段listId重复的数据

你如果只是简单的判断id相等然后取重比较容易,我给的具体做法是先排序后进行比对。

//原理:先对数组进行排序,如果相邻的两个元素不相等,则进一步进行判断来得出结果
function getRepeat(array,prop){

    var arr = array.concat([]);

    arr.sort(function(a,b){
        return a[prop] - b[prop]; 
    });

    var result = []; //结果

    for( var i = 0, item; item = arr[i]; ){

        var issame = false;

        for( var j = i+1, newItem; newItem = arr[j]; j++ ){

            i = j; //调整下一次比对时i开始的位置

            if( item[prop] == newItem[prop] ){ //判断是否相等
                issame = true;
                result.push(newItem);
            } else {
                if( issame ){ //将用来比对的元素(item)存入结果并跳出本次循环
                    issame = false;
                    result.push(item);
                }
                break;
            }

        }

        if( !arr[i+1] ) break; //结束循环

    }

    return result;

}

var arr = [{
    id : 2,
    name : "yy"
},{
    id : 3,
    name : "zz"
},{
    id : 2,
    name : "zz"
},{
    id : 3,
    name : "yy"
},{
    id : 21,
    name : "yy"
},,{
    id : 21,
    name : "aa"
},,{
    id : 32,
    name : "yy"
}];

console.log(getRepeat(arr,"id"));

如果对象要判断全等就不容易了,你得遍历比对属性,这个你可以自己去试。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏