从forEach推送对象后,数组保持为空 [英] Array keep empty after push object from forEach
本文介绍了从forEach推送对象后,数组保持为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要帮助,我遇到问题数组保持为空从forEach推入对象后,我会错过任何东西吗?这是代码:
Need Help, I have problem Array Keep Empty After pushing object from forEach, do I miss something ? here's the code :
const allStatus = [];
result.forEach(async (element) => {
const count = await BpCandidate.count({
where: {
applicationStatusId: element.id
},
raw: true,
});
})
record = {
id: element.id,
name: element.name,
code: element.code,
description: element.description,
total: count
};
allStatus.push(record);
});
console.log(allStatus);
预先感谢
推荐答案
使用 for ... of
可以为您工作,但是如果您使用 Promise.all,可以在这里看到改进.
Using for...of
is working for you, but can see an improvement here if you use Promise.all
const allStatus = await Promise.all(result.map(async (element) => {
const count = await BpCandidate.count({
where: {
applicationStatusId: element.id
},
raw: true,
});
return {
id: element.id,
name: element.name,
code: element.code,
description: element.description,
total: count
};
}))
这篇关于从forEach推送对象后,数组保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文