如何运行这个循环直到数组中没有更多的对象?

huangapple go评论123阅读模式
英文:

How do I run this loop until there are no more objects in the array?

问题

当数组达到 [] 时,我试图运行一个 while 循环,但它会崩溃。

这是我正在运行的代码:

  1. const mineflayer = require('mineflayer');
  2. let contas = require('./accounts');
  3. let fila = []
  4. function sleep(ms) {
  5. return new Promise(resolve => setTimeout(resolve, ms));
  6. }
  7. function main() {
  8. async function start(conta) {
  9. await sleep(1000);
  10. fila.logged = true;
  11. fila.shift();
  12. }
  13. async function queueStarter() {
  14. let loop = 1;
  15. if (fila.length >= loop && fila.length != 'undefined') {
  16. while (fila[0].logged == false) {
  17. start(fila[0]);
  18. await sleep(4000);
  19. }
  20. } else console.log('Reached array end')
  21. }
  22. for (key in contas) {
  23. let conta = contas[key];
  24. fila.push(conta);
  25. }
  26. queueStarter()
  27. }
  28. main();

这是控制台输出:

  1. PS C:\Users\Zwei\.vscode\IDKHowToCode> node .\StackExcample.js
  2. C:\Users\Zwei\.vscode\IDKHowToCode\StackExcample.js:20
  3. while (fila[0].logged == false){
  4. ^
  5. TypeError: Cannot read properties of undefined (reading 'logged')
  6. at queueStarter (C:\Users\Zwei\.vscode\IDKHowToCode\StackExcample.js:20:20)
  7. Node.js v18.12.1

我希望它在 fila 数组中没有更多对象时停止。为什么它在应该停止时没有停止呢?

英文:

I'm trying to run a while loop, but when the array reaches [], it crashes.

This is what I'm running:

  1. const mineflayer = require('mineflayer');
  2. let contas = require('./accounts');
  3. let fila = []
  4. function sleep(ms) {
  5. return new Promise(resolve => setTimeout(resolve, ms));
  6. }
  7. function main() {
  8. async function start(conta) {
  9. await sleep(1000);
  10. fila.logged = true;
  11. fila.shift();
  12. }
  13. async function queueStarter() {
  14. let loop = 1;
  15. if (fila.length >= loop && fila.length != 'undefined') {
  16. while (fila[0].logged == false) {
  17. start(fila[0]);
  18. await sleep(4000);
  19. }
  20. } else console.log('Reached array end')
  21. }
  22. for (key in contas) {
  23. let conta = contas[key];
  24. fila.push(conta);
  25. }
  26. queueStarter()
  27. }
  28. main();

This is the console:

  1. PS C:\Users\Zwei\.vscode\IDKHowToCode> node .\StackExcample.js
  2. C:\Users\Zwei\.vscode\IDKHowToCode\StackExcample.js:20
  3. while (fila[0].logged == false){
  4. ^
  5. TypeError: Cannot read properties of undefined (reading 'logged')
  6. at queueStarter (C:\Users\Zwei\.vscode\IDKHowToCode\StackExcample.js:20:20)
  7. Node.js v18.12.1

What I want is for it to stop when there are no more objects in the 'fila' array.

Why is it not stopping when it should?

答案1

得分: 0

@Barmar 建议了这个修复方法,而且它有效 如何运行这个循环直到数组中没有更多的对象?

queueStarter 函数中,将以下代码段:

while (fila[0].logged == false) {

修改为:

while (fila[0] && fila[0].logged == false){

这样会检查 fila[0] 是否存在并且是否已经被记录。

英文:

@Barmar suggested this fix and it works 如何运行这个循环直到数组中没有更多的对象?

on the queueStarter function, changing

while (fila[0].logged == false) {

to

while (fila[0] && fila[0].logged == false){

makes it check if there's something on fila[0] and if it's logged.

huangapple
  • 本文由 发表于 2023年1月11日 01:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75073972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定