gpt4 book ai didi

arrays - 值未插入到数组中,数组返回空白值

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:25 25 4
gpt4 key购买 nike

我有以下代码,问题是 obj 没有插入数组 arr 中。

let arr=[];
let frommilisec=1620000000;
let tomilisec=4680000000;
let slotmilisec=900000;
while(frommilisec<=tomilisec)
{
let timeslot_milisec=frommilisec+slotmilisec;

clinicslotsfunc(timeslot_milisec,maximumAppointment,clinicid,dated).then(results=>
{
let obj=results[0];
console.log(obj); // this displays { slot: '12:30:00', isbooked: 1 }
arr.push(obj);
});
console.log(arr) //this logs blank array
frommilisec=frommilisec+timeslot_milisec;
}

results[0] 包含数据 { slot: '12:30:00', isbooked: 1 }

最佳答案

Promise 是一个异步操作,因此您需要await异步操作的结果:

async yourmethod()
{
let arr=[];
let frommilisec=1620000000;
let tomilisec=4680000000;
let slotmilisec=900000;
while(frommilisec<=tomilisec)
{
let timeslot_milisec=frommilisec+slotmilisec;
let result = await clinicslotsfunc(timeslot_milisec
, maximumAppointment, clinicid, dated);

arr.push(result);
});
console.log(arr);
frommilisec=frommilisec+timeslot_milisec;
return arr;
}

关于arrays - 值未插入到数组中,数组返回空白值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59476582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com