Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于索引确定类变量名_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 基于索引确定类变量名

Javascript 基于索引确定类变量名,javascript,angular,typescript,Javascript,Angular,Typescript,我的情况是这样的: 我正在调用一个接受索引的函数,我需要返回一个对象的变量名。 我的代码与下面的代码类似: switch (boardIndex) { case 0: return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys1); case 1: return this.returnPlatformStatus(this.loginObjArr[arrIndex].s

我的情况是这样的: 我正在调用一个接受索引的函数,我需要返回一个对象的变量名。 我的代码与下面的代码类似:

  switch (boardIndex) {
      case 0:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys1);
      case 1:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys2);
      case 2:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys3);
      case 3:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys4);
      case 4:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys5);
      case 5:
        return this.returnPlatformStatus(this.loginObjArr[arrIndex].sys6);
这一个类似于[0-20]中的索引范围。我受够了每次都放这个开关箱。
有什么方法可以基于索引构造变量名吗?

您可以重新构造数据结构,使其更简单,使该对象成为一个数组,或者您可以通过简单的字符串连接形成属性名:

return this.returnPlatformStatus(this.loginJobArr[arrIndex]["sys" + (boardIndex + 1)]);

JavaScript中的对象属性名称始终可以通过字符串表达式动态创建。在本例中,
“sys”+(boardIndex+1)
将创建字符串,如发布的代码、
“sys1”
“sys2”
等。
[]
操作符允许对象的属性访问,就像它对数组的访问一样;毕竟,数组是对象。

Hi Pointy,这将是一个巨大的变化。。。重构结构是不可行的。@Sarwan那么你可以按照我在答案中的建议做:用简单的字符串连接形成属性名。