繁体   English   中英

两个数组之间除一个属性外的对象的交集

[英]intersection of objects except for one attribute between two arrays

我有两个数组,它们包含相同类型的对象(相同的属性但关联的值不同)。 我想比较两个数组并匹配除一个属性外相等的对象。 然后我想在第一个数组中找到匹配对象的索引,以便将这些对象推送到不同的数组中。

我认为所有这些都可以使用 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)
    }
}

在这种情况下,我想匹配除“foot”之外具有相同属性的对象,因此预期输出为:

//Expected Output:
this.MatchedPlayers = [
    {weight:'75',height:'170', foot:'Left', available:true}, 
    {weight:'65',height:'163', foot:'Right', available:false}
]

您可以采用简化的方法并省略foot属性,并通过_.isEqual获取左上方属性的交集。

 var a = [{ 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 }], b = [{ weight: '75', height: '170', foot: '', available: true }, { weight: '93', height: '201', foot: '', available: true }, { weight: '65', height: '163', foot: '', available: false }], omitFoot = o => _.omit(o, 'foot'), intersection = _.intersectionWith( _.map(a, omitFoot), _.map(b, omitFoot), _.isEqual ); console.log(intersection);
 .as-console-wrapper { max-height: 100% !important; top: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

要创建具有自定义行为的交集,请使用_.intersectionWith() 该方法接受在两个对象之间进行比较的谓词。 在这种情况下,比较几乎与_.isEqual()相同,但它应该忽略一个属性。

您可以使用_.isEqualWith()创建谓词,它接受自定义程序函数。 如果比较的键(第三个参数)是foot它应该返回true (相等,因为我们不在乎),否则它应该返回undefined以便_.isEqualWith()可以进行标准的_.isEqual()比较。

 const a = [{ 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 }]; const b = [{ weight: '75', height: '170', foot: '', available: true }, { weight: '93', height: '201', foot: '', available: true }, { weight: '65', height: '163', foot: '', available: false }]; const result = _.intersectionWith(a, b, _.partialRight(_.isEqualWith, (...args) => args[2] === 'foot' ? true : undefined ) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM