简体   繁体   English

javascript 中的动态多维数组 - 计数不起作用

[英]Dynamic, multidimensional array in javascript - counting up not working

I need a 3 dimensional array to count up - it needs to grow dynamically.我需要一个 3 维数组来计数 - 它需要动态增长。 It consists of index, string1 and string2.它由 index、string1 和 string2 组成。

This outputs exactly, what I want (for a single loop, since the array is just hardcoded to index 0)这完全输出了我想要的(对于单个循环,因为数组只是硬编码为索引 0)

   var otr_entries=[[0,"",""]];
   var otr_entries_count=0;

   some_working_for_loop()
   {
      if(is_important_value_to_save())
      {
         //otr_entries_count=otr_entries_count+1;
         otr_entries[otr_entries_count][1]=xx[i].previousElementSibling.innerHTML;
         otr_entries[otr_entries_count][2]=xx[i].innerHTML;
         window.alert(otr_entries[otr_entries_count][1]); // Expected output
         window.alert(otr_entries[otr_entries_count][2]); // Expected output
      }
   }

but when I replace otr_entries[0][2] with otr_entries[otr_entries_count][2] the script suddenly fails, if the count is not 0. That means, that the array is not just growing.但是当我用otr_entries[otr_entries_count][2]替换otr_entries[0][2]时,如果计数不为 0,脚本会突然失败。这意味着,数组不仅仅是在增长。 So how can this be archived?那么如何归档呢?

   var otr_entries=[[0,"",""]];
   var otr_entries_count=0;

   just_some_perfectly_working_for_loop(;;)
   {
      if(is_important_value_to_save())
      {
         otr_entries_count=otr_entries_count+1; // Counting up breaks the code
         otr_entries[otr_entries_count][1]=xx[i].previousElementSibling.innerHTML;
         otr_entries[otr_entries_count][2]=xx[i].innerHTML;
         window.alert(otr_entries[otr_entries_count][1]); // No output, script totally stops
         window.alert(otr_entries[otr_entries_count][2]); // No output, script totally stops
      }
   }

EDIT:编辑:

This is my solution, thanks to peters help.这是我的解决方案,感谢彼得斯的帮助。 Works perfectly fine.工作得很好。

   var otr_entries=[];
   var otr_entries_count=-1;

   some_working_for_loop()
   {
      if(is_important_value_to_save())
      {
         otr_entries_count=otr_entries_count+1;
         otr_entries.push(otr_entries_count,xx[i].previousElementSibling.innerHTML,xx[i].innerHTML)
         window.alert(otr_entries[otr_entries_count][1]); // Expected output
         window.alert(otr_entries[otr_entries_count][2]); // Expected output
      }
   }

You're just incrementing the counter before you add new item in the array.在数组中添加新项目之前,您只是增加了计数器。 The solution is to move the counter incrementing line at the end:解决方法是在末尾移动计数器递增线:

This worked for me这对我有用

const otr_entries=[[0,"",""]];
const otr_entries_count=0;

for(const entry of otr_entries) {
    // condition
    if(true) {
        otr_entries[otr_entries_count][1]='something';
        otr_entries[otr_entries_count][2]='something else'
        window.alert(otr_entries[otr_entries_count][1]);
        window.alert(otr_entries[otr_entries_count][2]);
        otr_entries_count+=1;
    }
}

If in the loop you're trying to add to the array you need to do otr_entries.push([count,"something","something2"]).如果在循环中您尝试添加到数组中,则需要执行 otr_entries.push([count,"something","something2"])。

Also, if you're adding to the array you shouldn't be using that same array as your loop control.此外,如果您要添加到数组中,则不应使用与循环控件相同的数组。

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

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