删除重复项javascript数组

时间:2015-07-29 22:29:57

标签: javascript node.js express

我有这两个数组

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];
    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }];

我想删除所有重复的结果,两者都只留下:

  [{ name: 'placefive', leagueID: '9722578' },
  { _id: 55b7f4825d3255b043e3dfe8, name: 'placesix', leagueID: '9722578', __v: 0 }]

我有以下功能,但是如果你咆哮错误的树,我不需要重复使用任何一个:)

var unquie = function (array1, array2, name) {
    var myArr = array1.concat(array2);
    var newArr = myArr;

    for(var h = 0; h < myArr.length; h++) {
        var curItem = myArr[h][name];
        var foundCount = 0;
        // search array for item
        for(var i = 0; i < myArr.length; i++) {
            if (myArr[i][name] === myArr[h][name])
                foundCount++;
        }
        if(foundCount > 1) {
            // remove repeated item from new array
            for(var j = 0; j < newArr.length; j++) {
                if(newArr[j][name] === curItem) {
                    newArr.splice(j, 1);
                    j = j - 1;
                }
            }
        }
    }

    return newArr;
};
unquie(array1, array2, 'name');
//Random incorrect results :(

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];

    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }]


console.info('Original Arrays');
console.info(array1);
console.info(array2);
    
    var unquie = function (array1, array2, name) {
    	var myArr = array1.concat(array2);
    	var newArr = myArr;
    	
    	for(var h = 0; h < myArr.length; h++) {
    		var curItem = myArr[h][name];
    		var foundCount = 0;
          
    		for(var i = 0; i < myArr.length; i++) {
    			if (myArr[i][name] === myArr[h][name])
    				foundCount++;
    		}
    		if(foundCount > 1) {
    			// remove repeated item from new array
    			for(var j = 0; j < newArr.length; j++) {
    				if(newArr[j][name] === curItem) {
    					newArr.splice(j, 1);
    					j = j - 1;
    				}
    			}
    		}
    	}
    
    	return newArr;
    };

    console.info('Converted Arrays');
    console.info(unquie(array1, array2, 'name'));
    //

4 个答案:

答案 0 :(得分:1)

使用filter()将数组限制为另一个数组中缺少的元素。

使用JSON.stringify()帮助比较两个对象。

这是一个功能齐全的方法:

var array1 = 
      [{name: 'placeone', leagueID: '8368223' },
       {name: 'placetwo', leagueID: '6164631' },
       {name: 'placethree', leagueID: '4564836' },
       {name: 'placefour', leagueID: '9722578' },
       {name: 'placefive', leagueID: '9722578' }
      ];

var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
       {name: 'placetwo', leagueID: '6164631' },
       {name: 'placethree', leagueID: '4564836' },
       {name: 'placefour', leagueID: '9722578' },
       {name: 'placesix', leagueID: '9722578' }
      ];

function unique(a1, a2) {
  function comp(a1, a2) {
    return a1.filter(function(val1) {
      return !a2.filter(function(val2) {
        return JSON.stringify(val1)==JSON.stringify(val2)
      }).length;
    });
  }
  return comp(a1, a2).concat(comp(a2, a1));
}

var array3 = unique(array1, array2);
document.body.innerHTML= JSON.stringify(array3);

&#13;
&#13;
dir-one
&#13;
&#13;
&#13;

答案 1 :(得分:1)

另一种解决方案:

var array1 = 
      [ { name: 'placeone', leagueID: '8368223' },
      { name: 'placetwo', leagueID: '6164631' },
      { name: 'placethree', leagueID: '4564836' },
      { name: 'placefour', leagueID: '9722578' },
      { name: 'placefive', leagueID: '9722578' }];

    var array2 = 
      [{name: 'placeone', leagueID: '8368223' },
      {name: 'placetwo', leagueID: '6164631' },
      {name: 'placethree', leagueID: '4564836' },
      {name: 'placefour', leagueID: '9722578' },
      {name: 'placesix', leagueID: '9722578' }]


console.info('Original Arrays');
console.info(array1);
console.info(array2);

    var unquie = function (array1, array2, propName) {
        var myArr = array1.concat(array2);
        var newArr = [];

        for (var i = 0; i < myArr.length; i++) {
          var dupIndex = -1;
          var item = myArr[i];

          for (var j = 0; j < newArr.length; j++) {
            if (item[propName] == newArr[j][propName]) {
              dupIndex = j;
              break;
            }
          }

          if (dupIndex >= 0) {
            newArr.splice(dupIndex, 1);
          } else {
            newArr.push(item);
          }
        }

        return newArr;
    }

    console.info('Converted Arrays');
    console.info(unquie(array1, array2, 'name'));

答案 2 :(得分:1)

这可能不是最干净的方式,但它确实有效。我遍历第一个数组,在第二个数组中查找值,如果找不到,则将其添加到输出数组中。然后再次循环,这次是在第二个数组上

for (var i = 0; i < array1.length; i++) {
    var tmp = array2.filter(function (aa) { return aa.name == array1[i].name });
    if ( !tmp.length )
        array.push( array1[i] )
}
for (var i = 0; i < array2.length; i++) {
    var tmp = array1.filter(function (aa) { return aa.name == array2[i].name });
    if ( !tmp.length )
        array.push( array2[i] )
}

我正在寻找重复的name属性,但您可以将任何属性或对象作为一个整体进行比较。

http://jsfiddle.net/daCrosby/j2553xcz/

这是一个函数:http://jsfiddle.net/daCrosby/j2553xcz/1/

答案 3 :(得分:0)

我提出了另一种方法来实现这一点,如果它们是重复的,而不是从数组中删除记录,如果它不存在则将它更容易添加到新数组中:

var unquie = function (array1, array2, name) {
    var myArr = array1.concat(array2);
    var newArr = [];

    for(var h = 0; h < myArr.length; h++) {
        var curItem = myArr[h][name];
        var exist = false;
        // search if item already included
        for(var i = 0; i < newArr.length; i++) {
            if (newArr[i][name] === myArr[h][name])
                exist = true;
        }
        if(!exist) {
            // add item if not included
            newArr.push(myArr[h]);
        }
    }

    return newArr;
};
unquie(array1, array2, 'name');