【问题标题】:I cannot set while to work properly in for loops in ts我无法设置 while 在 ts 中的 for 循环中正常工作
【发布时间】:2020-04-24 20:25:05
【问题描述】:

我想获得具有唯一值的数组数组,但在某些时候

同时

被遗漏或忽略(我推测是因为它的异步性质)。有人可以帮忙添加一个承诺,或者设置

异步/等待

结构或提供更好的建议如何检查数组。我尝试添加

异步/等待

但我得到了错误,我不确定我可以在哪里添加或使用它。这是

getSeveralArrays() {
 for (let index = 0; index < 50; index++) {
        this.getValue();
      }
}
getValue() {

    this.i++;

    this.array = [];
    this.randomArray = [];
for (let index = 0; index < 4; index++) {

    this.randomValue = this.getRandom();

    if (this.array.length === 2) {

        while ((this.array[0] === this.randomValue) || (this.array[1] === this.randomValue)) {
            this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);


    } else if (this.array.length === 3) {

        while ((this.array[0] === this.randomValue) || (this.array[1] === this.randomValue) || (this.array[2] === this.randomValue)) {
            this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);


    } else {

        this.array.push(this.randomValue);

    }

    console.log({...this.array});

    this.randomArray.push({ind: this.i, val: this.array});

    }
}


  getRandom() {

      const value = Math.floor(Math.random() * 4);
      return value;
  }

【问题讨论】:

  • 没有必要使用async 代码,因为您没有使用Promise。您能否将所需的结果写为文本,将源数据写为文本。了解您想做什么会非常有帮助。

标签: arrays typescript loops asynchronous while-loop


【解决方案1】:

您的所有代码中都没有异步,因此不需要async/await。此外,while 不是异步的。

不过,您缺少长度为 1 的 case,因此第二个元素可能始终与第一个元素相同。

class X {
  getSeveralArrays() {
    for (let index = 0; index < 50; index++) {
      this.getValue();
    }
  }
  getValue() {
    this.i++;

    this.array = [];
    this.randomArray = [];
    for (let index = 0; index < 4; index++) {
      this.randomValue = this.getRandom();

      if (this.array.length === 1) {
        while (this.array[0] === this.randomValue) {
          this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);
      } else if (this.array.length === 2) {
        while (this.array[0] === this.randomValue || this.array[1] === this.randomValue) {
          this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);
      } else if (this.array.length === 3) {
        while (
          this.array[0] === this.randomValue ||
          this.array[1] === this.randomValue ||
          this.array[2] === this.randomValue
        ) {
          this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);
      } else {
        this.array.push(this.randomValue);
      }

      console.log({ ...this.array });

      this.randomArray.push({ ind: this.i, val: this.array });
    }
  }

  getRandom() {
    const value = Math.floor(Math.random() * 4);
    return value;
  }
}

console.log(new X().getSeveralArrays());

此外,所有这些检查都可以简化:

      while (this.array.some(value => value === this.randomValue)) {
        this.randomValue = this.getRandom();
      }

通常,就像一个小的代码审查一样:如果您不需要从另一个类方法(或外部某个地方)访问某些内容,则不应将所有这些值放入类属性中,而应将它们作为值保留在函数中.

所以this.randomValuethis.array 应该在你的函数中定义为letconst

【讨论】:

  • 非常感谢您的回答!此外,您的简化方法更好,更具可读性。祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2013-06-26
  • 2020-10-27
相关资源
最近更新 更多