【发布时间】:2019-12-18 15:35:44
【问题描述】:
我有两个数组,它们包含相同类型的对象(相同的属性但关联的值不同)。我想比较两个数组并匹配除一个属性外相等的对象。然后我想在第一个数组中找到匹配的对象的索引,以便将这些对象推送到不同的数组中。
我认为所有这些都可以使用 lodash 完成,并且我想避免 for 循环,以使其尽可能高效
请注意,我在一个 js 类中工作
这是我的尝试(不工作)
class MatchPlayers {
constructor(){
this.TeamA = [
{weight:'75',height:'170', foot:'Left', available:true},
{weight:'88',height:'190', foot:'Right', available:true},
{weight:'65',height:'163', foot:'Right', available:false},
{weight:'70',height:'168', foot:'Left', available:true}
]
this.TeamB = [
{weight:'75',height:'170', foot:'', available:true},
{weight:'93',height:'201', foot:'', available:true},
{weight:'65',height:'163', foot:'', available:false}
]
this.MatchedPlayers = []
}
PlayersMatch (){
for(this.i=0;this.i<this.TeamA.length;this.i++){
if (_.intersection(this.TeamA,{weight:this.TeamB.weight, height:this.TeamB.height, available:this.TeamB.available})){
this.position = _.findIndex(this.TeamA,{weight:this.TeamB.weight, height:this.TeamB.height, available:this.TeamB.available})
this.MatchedPlayers.push(this.TeamA[this.position])
} else {console.log('No matchable players')}
}
console.log(this.MatchedPlayers)
}
}
在这种情况下,我想匹配除“脚”之外具有相同属性的对象,因此预期输出为:
//Expected Output:
this.MatchedPlayers = [
{weight:'75',height:'170', foot:'Left', available:true},
{weight:'65',height:'163', foot:'Right', available:false}
]
【问题讨论】:
-
如果你想使用 lodash 交集,你可以使用
_.intersectionBy或_.intersectionWith。第二个可能更适合您的用例 -
您必须为
_.intersectionWith编写一个自定义函数,该函数只比较您想要比较的属性,或者比较除您不想要的属性之外的所有属性。 -
我试过了,但它不起作用。但是我也可以使用不同的功能
标签: javascript arrays lodash