【发布时间】: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