【发布时间】:2020-08-29 01:54:16
【问题描述】:
我在使用 Javascript 执行方法时遇到问题。我有这段代码:
let array = [];
list.forEach((item_1) => {
let variable = 0;
fetch("data.json")
.then((resp) => resp.json())
.then((data) => {
data.forEach((item_2) => {
item_2.forEach((item_3) => {
item_3forEach((item_4) => {
if (condition) {
variable += 1;
}
});
});
});
});
array.push({a:variable, b: item_1});
});
我需要仅在迭代完成后追加变量,但是当我运行项目时,变量首先追加(初始值为 0),然后进行迭代。 我怎样才能让它只在进程完成时才追加?
编辑: 由于我之前不是很清楚,我分享我提出问题的一段代码
for (let c = -100; c < 501; c += 100) {
fetch(`data/votes_${c + 101}-${c + 200}.json`)
.then((resp) => resp.json())
.then((data_vot) => {
proy_etiq.forEach((eti_selec) => {
let cont_si_tot = 0;
data_vot.congresista.forEach((congresista) => {
congresista.proyecto.forEach((proyecto) => {
let cont_si = 0;
eti_selec.proyectos.forEach((proy_selec) => {
if (proyecto.id === proy_selec.id) {
cont_si += proyecto.voto.si;
proy_selec.votos[0] += cont_si;
}
});
cont_si_tot += cont_si;
});
});
flor_si.push({ Tema: eti_selec.nombre, abs: cont_si_tot });
});
});
}
proy_etiq 是我已经拥有的列表,
【问题讨论】:
标签: javascript iteration order-of-execution