gpt4 book ai didi

javascript - 删除两个重复的项目 javascript 数组

转载 作者:行者123 更新时间:2023-11-28 19:05:49 25 4
gpt4 key购买 nike

我有这两个数组

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'));
//

最佳答案

另一个解决方案:

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'));

关于javascript - 删除两个重复的项目 javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com