【问题标题】:How to use dynamic variable names in ionic如何在 ionic 中使用动态变量名
【发布时间】:2018-01-30 21:02:01
【问题描述】:

在我的 ionic 应用程序中,我观看了一个复制到我的本地变量 currentGame 中的 firebase 路径“游戏”。 示例路径:

this.gameProvider.currentGame.round2.character4.somevalue

我最多有 10 轮。 我该怎么做类似的事情

for (i=1; i<10; i++) {
  console.log(this.gameProvider.currentGame.round + i + .character4.somevalue);
}

【问题讨论】:

    标签: javascript firebase ionic-framework


    【解决方案1】:

    使用bracket notation

    gameProvider = {currentGame:{}}
    
    for (i=1; i<10; i++) {
      this.gameProvider.currentGame['round' + i ] = {character4: {somevalue: 'asdf'}};
    }
    
    for (i=1; i<10; i++) {
      console.log(this.gameProvider.currentGame['round' + i ].character4.somevalue);
    }

    【讨论】:

    • 完美答案!谢谢:-)
    猜你喜欢
    • 1970-01-01
    • 2011-07-04
    • 2012-03-03
    • 2012-06-18
    • 2021-12-15
    • 2011-06-29
    相关资源
    最近更新 更多