首页 > 解决方案 > 打字稿:如果条件为真,则在数组中添加项目

问题描述

我想finalObject.members使用来自另一个数组的值填充一个数组allParticipants。第二个数组 ( allParticipants) 如下所示:

allParticipants = [
    { 
        uid:"mem1_100_00",
        member: "mem1",
        tontine: "100"
        total: 785
    },
    { 
        uid:"mem1_100_01",
        member: "mem1",
        tontine: "100"
        total: 800
    },
    { 
        uid:"mem1_200_00",
        member: "mem1",
        tontine: "200"
        total: 1000
    },
    {
        uid:"mem2_100_00",
        member: "mem2",
        tontine: "100"
        total: 200
    },
    { 
        uid:"mem2_200_00",
        member: "mem2",
        tontine: "200"
        total: 7850
    },
    { 
        uid:"mem2_200_01",
        member: "mem2",
        tontine: "200"
        total: 5000
    },
    { 
        uid:"mem2_200_02",
        member: "mem2",
        tontine: "200"
        total: 1600
    },
    { 
        uid:"mem3_100_00",
        member: "mem3",
        tontine: "100"
        total: 150
    },
    { 
        uid:"mem3_100_01",
        member: "mem3",
        tontine: "100"
        total: 0
    },
    { 
        uid:"mem3_200_00",
        member: "mem3",
        tontine: "200"
        total: 2500
    }

]

插入后第一个数组 ( finalObject.members) 应该如下所示:

finalObject.members = [
    { 
        uid: "mem1",
        tontines: {
            100:[
                {
                    uid: "mem1_100_00",
                    total:785
                },
                {
                    uid: "mem1_100_01",
                    total:800
                },
            ],
            200:[
                {
                    uid: "mem1_200_00",
                    total:1000
                }
              ]
        }
    },
    { 
        uid: "mem2",
        tontines: {
            100: [
                {
                    uid: "mem2_100_00",
                    total: 200
                }
            ],
            200:[
                {
                    uid: "mem2_200_00",
                    total: 7850
                },
                {
                    uid: "mem2_200_01",
                    total: 5000
                },
                {
                    uid: "mem2_200_02",
                    total: 1600
                }
            ]
        }
    },
    { 
        uid: "mem3",
        tontines: {
            100: [
                {
                    uid: "mem3_100_00",
                    total: 150                
                },
                {
                    uid: "mem3_100_01",
                    total: 0                
                }
            ],
            200:[
                {
                    uid: "mem3_200_00",
                    total: 2500        
                }
            ]
        }
    }
]

所以我写了这段代码:

const sizMem    = finalObject.members.length;
const sizPartp  = allParticipants.length;

for(let idx1=0; idx1<sizPartp; idx1++){
    let partP = allParticipants[idx1]
    for(let idx2=0; idx2<sizMem; idx2++){
        let memP = finalObject.members[idx2];
        if(partP.member.localeCompare(memP.uid) == 0){
            finalObject.members[idx2].tontines[partP.tontine].push({
                uid:     partP.uid,
                total:   partP.total,
            })
            break
        }
    }
}

但这就是我得到的:

finalObject.members = [
    { 
        uid: "mem1",
        tontines: {
            100:[
                {
                    uid: "mem1_100_00",
                    total:785
                },
                {
                    uid: "mem1_100_01",
                    total:800
                },
                {
                    uid: "mem2_100_00",
                    total: 200
                },
                {
                    uid: "mem3_100_00",
                    total: 150                
                },
                {
                    uid: "mem3_100_01",
                    total: 0                
                }                    
            ],
            200:[
                {
                    uid: "mem1_200_00",
                    total:1000
                },
                {
                    uid: "mem2_200_00",
                    total: 7850
                },
                {
                    uid: "mem2_200_01",
                    total: 5000
                },
                {
                    uid: "mem2_200_02",
                    total: 1600
                },
                {
                    uid: "mem3_200_00",
                    total: 2500        
                }                    
              ]
        }
    },
    { 
        uid: "mem2",
        tontines: {
            100:[
                {
                    uid: "mem1_100_00",
                    total:785
                },
                {
                    uid: "mem1_100_01",
                    total:800
                },
                {
                    uid: "mem2_100_00",
                    total: 200
                },
                {
                    uid: "mem3_100_00",
                    total: 150                
                },
                {
                    uid: "mem3_100_01",
                    total: 0                
                }                    
            ],
            200:[
                {
                    uid: "mem1_200_00",
                    total:1000
                },
                {
                    uid: "mem2_200_00",
                    total: 7850
                },
                {
                    uid: "mem2_200_01",
                    total: 5000
                },
                {
                    uid: "mem2_200_02",
                    total: 1600
                },
                {
                    uid: "mem3_200_00",
                    total: 2500        
                }                    
              ]
        }
    },
    { 
        uid: "mem3",
        tontines: {
            100:[
                {
                    uid: "mem1_100_00",
                    total:785
                },
                {
                    uid: "mem1_100_01",
                    total:800
                },
                {
                    uid: "mem2_100_00",
                    total: 200
                },
                {
                    uid: "mem3_100_00",
                    total: 150                
                },
                {
                    uid: "mem3_100_01",
                    total: 0                
                }                    
            ],
            200:[
                {
                    uid: "mem1_200_00",
                    total:1000
                },
                {
                    uid: "mem2_200_00",
                    total: 7850
                },
                {
                    uid: "mem2_200_01",
                    total: 5000
                },
                {
                    uid: "mem2_200_02",
                    total: 1600
                },
                {
                    uid: "mem3_200_00",
                    total: 2500        
                }                    
              ]
        }
    }
]

因此它为每个成员添加所有元素,而不是仅将新元素添加到相应的成员。我检查了if条件(但将结果写到控制台),这部分一切正常。member仅当of的属性与 a 的属性Participant相同时才执行插入。然而,它到处都添加了新元素!uidmember

我哪里做错了?

标签: arraystypescript

解决方案


无耻的插头。我的小型、无依赖库blinq非常适合这些类型的转换。

const output = blinq(allParticipants)
  .groupBy(x => x.member)
  .select(g => ({
    uid: g.key,
    member: Object.fromEntries(
      g
      .groupBy(x => x.tontine)
      .select(gg => [
        gg.key,
        gg.select(x => ({
          uid: x.uid,
          total: x.total
        })).toArray()
      ])
      .toArray()
    )
  }))
  .toArray();

const {
  blinq
} = window.blinq
const allParticipants = [{
    uid: "mem1_100_00",
    member: "mem1",
    tontine: "100",
    total: 785
  },
  {
    uid: "mem1_100_01",
    member: "mem1",
    tontine: "100",
    total: 800
  },
  {
    uid: "mem1_200_00",
    member: "mem1",
    tontine: "200",
    total: 1000
  },
  {
    uid: "mem2_100_00",
    member: "mem2",
    tontine: "100",
    total: 200
  },
  {
    uid: "mem2_200_00",
    member: "mem2",
    tontine: "200",
    total: 7850
  },
  {
    uid: "mem2_200_01",
    member: "mem2",
    tontine: "200",
    total: 5000
  },
  {
    uid: "mem2_200_02",
    member: "mem2",
    tontine: "200",
    total: 1600
  },
  {
    uid: "mem3_100_00",
    member: "mem3",
    tontine: "100",
    total: 150
  },
  {
    uid: "mem3_100_01",
    member: "mem3",
    tontine: "100",
    total: 0
  },
  {
    uid: "mem3_200_00",
    member: "mem3",
    tontine: "200",
    total: 2500
  }
];
const output = blinq(allParticipants)
  .groupBy(x => x.member)
  .select(g => ({
    uid: g.key,
    member: Object.fromEntries(
      g
      .groupBy(x => x.tontine)
      .select(gg => [
        gg.key,
        gg.select(x => ({
          uid: x.uid,
          total: x.total
        })).toArray()
      ])
      .toArray()
    )
  }))
  .toArray();
console.log(JSON.stringify(output, null, 2));
<script src="https://cdn.jsdelivr.net/npm/blinq"></script>


推荐阅读