1 回答
TA贡献1900条经验 获得超5个赞
我试图简化您的代码和reduce. 我已将 s 替换reduce为filters 和maps。请检查一下并告诉我这是否有帮助。
const mockScheduledOperation = {
Ranks: [{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
}
]
};
const getAllRewards = async () => {
const freeRewardsInventory =
([] as string[])
// converting multi-dimensional array into uni-dimensional
.concat(
...mockScheduledOperation.Ranks
.filter(rank => rank.FreeRewards.InventoryRewards.length)
.map(rank => (
rank.FreeRewards.InventoryRewards
// removing all falsy values
.filter(Boolean)
.map(item => item.InventoryItemPath)
)
)
);
const getItem = (rewardPath: string) => mockReturnedItem;
const freeRewardsRaw = await Promise.all(freeRewardsInventory.map(rewardPath => getItem(rewardPath)))
const formattedRewards = freeRewardsRaw
.map < ProgressionRewards[] > ((res) => {
const formattedReward: ProgressionRewards = {
// free = unlocked, paid = locked
locked: false,
level: null,
difficulty: res.CommonData.Quality || null,
date: res.CommonData.DateReleased.ISO8601Date || null,
rewardAttachments: [{
image: res.CommonData.DisplayPath.Media.MediaUrl.Path || null,
title: res.CommonData.Title.value || null,
description: res.CommonData.Description.value || null,
type: res.CommonData.Type || null,
released: null,
manufacturer: null,
howUnlock: null,
}, ],
};
return formattedReward;
}, []);
}
);
return formattedRewards;
};
添加回答
举报