【发布时间】:2018-06-13 14:47:30
【问题描述】:
我一直在努力解决我遇到的问题。我有一个包含对象的数组,如下所示:
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: "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)
}
})
但这只检查对象的值是否等于它们自身。
谁能帮帮我?
【问题讨论】:
-
array需要保留其原始值还是可变的?
标签: javascript arrays function sorting