简体   繁体   English

在JavaScript中使用动态键名推送数组值

[英]Push Array values with a dynamic keyname in javascript

I am using a function to push values to an array with two parameters.In this the second parameter indicates the level of arrays.so i need to push values to that array using this level params value. 我正在使用一个函数将值推入具有两个参数的数组中。在此,第二个参数指示数组的级别。因此,我需要使用此level参数值将值推入该数组。

Here is my code: 这是我的代码:

addLevel1(i, level) {
        let advArr1                                 =   {};
        let nextLevel                               =   parseInt(level)+1
        advArr1["level"+level+"_adv_url"]           =   "";
        advArr1["level"+level+"_custom_variables"]  =   "";
        advArr1["level"+level+"_percentage"]        =   "";
        advArr1["level"+level+"_status"]            =   "";
        advArr1["level"+nextLevel]                  =   [];
        this.containers[i].level1.push(advArr1);
        console.log(this.containers);
};

In this instread of 在这种理解中

this.containers[i].level1.push(advArr1);

I need dynamic vale for level1. 我需要第1级的动态谷歌。 ie if param level is 1 it should be 即,如果参数级别为1,则应为

this.containers[i].level1.push(advArr1);

and if param level is 2 it will be 如果参数级别为2,则为

this.containers[i].level2.push(advArr1);

The sense of objects is that you can easily look up properties by name . 对象的意义在于您可以轻松地按名称查找属性。 As you mix in the dynamic level variable into it, you defeat that purpose: 当您将动态级别变量混入其中时,您将达到以下目的:

  const levels = [
   { level1_status: "ok" },
   { level2_status: "wrong" }
 ];

 for(const level of levels) {
   // How to get the status here?
   level["level" + /*How to get the level?*/ "_status"]
 }

Now instead, you should have static keys, so all levels should have the same keys. 现在,您应该具有静态键,因此所有级别都应具有相同的键。 To differentiate different levels, you could use a level property : 要区分不同的级别,可以使用level属性

  const levels = [
   { level: 1, status: "ok" },
   { level: 2, status: "wrong" }
  ];

  for(const level of levels) {
    console.log("Level:", level.level); // isn't that simple?

    console.log("has Status:", level.status); // Wohoo!
  }

Now the next thing is that containers[i].level1 thing: If you would have a levels array property on the container, you could just access it as containers[i].levels[1] without any problems. 现在,接下来是containers[i].level1 :如果您在容器上具有一个levels数组属性,则可以将其作为containers[i].levels[1]而不会出现任何问题。 The main benefit is, that you can now go easily over the different levels of a container: for(const level of container.levels) . 主要好处是,您现在可以轻松地遍历容器的不同级别: for(const level of container.levels)

Under the assumption that the container has such an array, your code could be: 假设容器具有这样的数组,则您的代码可以是:

 addLevel(i, level){
    this.containers[i].levels[level] = {
      level,
      nextLevel: level + 1,
      advUrl: "",
      percentage: 0, // does make more sense than a string, right?
       status: "",
   };
 }

 // somewhere:
 context.addLevel(/*i*/ 0, /*level*/ 1);

您可以使用方括号而不是点符号来访问对象属性

this.containers[i][level].push(advArr1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM