【问题标题】:How to set a special rule in an array?如何在数组中设置特殊规则?
【发布时间】:2019-09-16 23:02:50
【问题描述】:

我有一个数组和一个计数器,与数组相匹配。

我需要的是具有以下属性的序列: 当计数器达到 x6 时,前 5 项应设置为 false。 当计数器达到 x11 时,前 10 项应设置为 false。 因此,只有 5 个项目在序列中一次设置为 true。

//in .ts for reference
//Counter, which is matched with the array - they are then saved:
this.counter+=1;
var xNum="x"+this.counter;
this.x[xNum] = true;

//Array of items to infinite
public x : any = ["x0", "x1", "x2", "x3", "x4", "x5", "x6",
  "x7", "x8", "x9", "x10", "x11", "x12",
  "x13", "x14", "x15", "x16", "x17", "x18"...]

【问题讨论】:

  • this.x[xNum] = true; 将是对数组的滥用,因为您本质上是在使用普通对象,但它没有很好的理由具有数组原型
  • 嗯,这部分确实有效——我的问题是关于如何设置序列规则。
  • 几点建议: 1. 不要将数组类型设为任何类型。 2.不要在数组上设置属性(this.x[xNum] = true),为此使用单独的对象{} 3.您的描述没有意义。最后只有 5 项与上面提到的 10 项相矛盾。
  • 计数器如何递增?
  • 为什么我不能将数组类型设为“any”?为什么要在数组中使用 {} 声明状态? “最后只有5个项目与上面提到的10个项目相矛盾。” - 请把上面写的再读一遍。

标签: javascript html css arrays typescript


【解决方案1】:

可能是这样的:

if(counter >= 5 && counter % 5 === 0){
  for(let i = this.counter - 5; i < counter; i++){
    const xNum = "x"+this.counter;
    this.x[xNum] = false;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 2010-11-10
    • 1970-01-01
    • 2015-09-29
    • 2013-01-11
    • 2020-12-01
    • 2019-11-03
    相关资源
    最近更新 更多