arrays - 打字稿:如果条件为真,则在数组中添加项目
问题描述
我想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
相同时才执行插入。然而,它到处都添加了新元素!uid
member
我哪里做错了?
解决方案
无耻的插头。我的小型、无依赖库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>
推荐阅读
- mysql - Sequelize top level where with Op.or 给出错误
- java - 用于写入密集型应用程序的 Java 并发集合 - 只需少量读取
- javascript - 添加文档后,如何在 React Native 中保存 Firestore 响应(可能未处理的承诺拒绝(id:1):TypeError:“res”是只读的)?
- swift - 如何使这些安全规则对 Firestore 满意
- javascript - 尝试运行代码时检查页面不起作用
- c - 如何将 DX7 中的 SetTexture 更新到 DX9?
- printing - 为什么 flang Fortran print 在一定宽度处添加新行?
- javascript - 将两个返回语句合二为一
- python - requests_html 安装没有解决警告
- java - Java ListStack push 和 pushAll() 方法