我一直试图解决我遇到的问题。我有一个包含对象的数组,如下所示:
var array = [
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Hannah Reed",
Country: "Scottland",
Age: 23
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
}
];
我希望获取具有重复值的对象,并根据要搜索的值。即,我想获得具有重复值的对象" name"和"年龄"但是nog" country"所以我最终会:
[
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
}
];
如果一直在尝试
array.forEach(function(name, age){
if(array.name == name || array.age == age){
console.log(the result)
}
})
但是只检查对象的值是否等于它们。
有人能帮助我吗?
答案 0 :(得分:7)
我参加聚会有点晚了,但这可能会帮助遇到相同问题的人,因为我认为这是一个更容易理解的解决方案:
let duplicates = [];
array.forEach((el, i) => {
array.forEach((element, index) => {
if (i === index) return null;
if (element.name === el.name && element.Age === el.Age) {
if (!duplicates.includes(el)) duplicates.push(el);
}
});
});
console.log("duplicates", duplicates);
要理解以下两点可能很棘手:
答案 1 :(得分:4)
您可以使用2 reduce
。第一个是对数组进行分组。第二个是仅包含具有多于1个元素的组。
var array = [{"name":"Steven Smith","Country":"England","Age":35},{"name":"Hannah Reed","Country":"Scottland","Age":23},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84},{"name":"Steven Smith","Country":"England","Age":35},{"name":"Robert Landley","Country":"England","Age":84}]
var result = Object.values(array.reduce((c, v) => {
let k = v.name + '-' + v.Age;
c[k] = c[k] || [];
c[k].push(v);
return c;
}, {})).reduce((c, v) => v.length > 1 ? c.concat(v) : c, []);
console.log(result);
答案 2 :(得分:3)
快速回答我现在来了:
let result = [];
for(let item of array)
{
for(let checkingItem of array)
{
if(array.indexOf(item) != array.indexOf(checkingItem) &&
(item.name == checkingItem.name || item.Age == checkingItem.Age))
{
if(result.indexOf(checkingItem) == -1)
{
result.push(checkingItem);
}
}
}
}
console.log(result);
答案 3 :(得分:2)
尝试下面的代码段。它通过它自己的元素(内部循环)循环数组。第一个if
条件检查相同的元素(我们不希望在输出中),第二个if
使所需的条件匹配以识别任何重复的对象。
var array = [
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Hannah Reed",
Country: "Scottland",
Age: 23
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
}
];
for(let obj of array){
for(let ele of array){
if(obj==ele)
continue;
if(ele.name===obj.name && ele.age===obj.age){
console.log(obj);
break;
}
}
}
答案 4 :(得分:1)
这是一个替代解决方案,我将指出
let array = getData(); // sets up data array
let duplicates = array.filter(duplicatesOnly); // filter out duplicates
console.log( duplicates ); // output to console
/* ============================================================================= */
// Returns true/false based on a duplicate object being found
function duplicatesOnly(v1, i1, self) {
let ndx = self.findIndex(function(v2, i2) {
// make sure not looking at the same object (using index to verify)
// use JSON.stringify for object comparison
return (i1 != i2 && JSON.stringify(v1) == JSON.stringify(v2))
})
return i1 != ndx && ndx != -1
}
// Use function hoisting to place trivial code at the bottom of example
function getData() {
return [{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Hannah Reed",
Country: "Scottland",
Age: 23
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
},
{
name: "Steven Smith",
Country: "England",
Age: 35
},
{
name: "Robert Landley",
Country: "England",
Age: 84
}
];
}

JSON.stringify
(非常昂贵)JSON.stringify
可以根据键顺序创建字符串,因此即使对象具有相同的键/值,顺序可能是比较对象时的一个因素 - 这可以被视为一个好处或成本filter
和findIndex
的一个例子,集中并不是为了提高效率。可以通过使用缓存并执行JSON.stringify
一次,或者通过更加自定义的方式完全避免它来改进它。