簡體   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