Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Arrays - Fatal编程技术网

如果数组中的元素在加载之前无法定义,如何加入数组JavaScript

如果数组中的元素在加载之前无法定义,如何加入数组JavaScript,javascript,arrays,Javascript,Arrays,我有如下JavaScript代码: var categorylength = k[E].category.length; var mm = k[E].category; for(var nhi=0; nhi <= categorylength; nhi++ ){ arraypostcate = mm[nhi].term; postcate = arraypostcate.join(",");

我有如下JavaScript代码:

  var categorylength = k[E].category.length;
     var mm = k[E].category;                

   for(var nhi=0; nhi <= categorylength; nhi++ ){
         arraypostcate = mm[nhi].term;
         postcate = arraypostcate.join(",");
    }
   alert(postcate );
               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);
如何获取PostState的值是为创建arraypostcate的元素字符串。 但我不知道该怎么做。我使用了join,但它不是正确的方法

               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);
注意:我不能直接使用:postate=k[E].category.join;因为无法创建元素 如果for循环针对每个元素运行

               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);

我想要的结果是:postate=k[E].category.join;但是与for循环一起使用。

我无法正确理解这个问题,但似乎这就是您要做的:

               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);
var postate=

               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);
var categorylength=k[E]。category.length; var-mm=k[E]。类别

               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);

它对您有用吗?

我需要在每个for循环之后加入元素。:。如果不查看数据,很难完全理解,但似乎您只想从每个mm[nhi].term值中创建一个字符串,并用逗号分隔。要在循环中执行此操作,请在循环变量my_Array=[];,之前创建一个空数组;,然后在循环中执行我的_array.pushmm[nhi].term;,然后在循环结束后,执行alertmy_array.join,;嗨,斜视。谢谢你的好方法。非常感谢你。请加上你的答案,我会给你最好的评分。嗨,绝地武士。它可以工作,但有未定义的元素。非常感谢。
               for(var nhi=0; nhi < categorylength; nhi++ ){
                     arraypostcate = mm[nhi].term;
                     if(nhi === categorylengh-1) postcate += arraypostcate;
                         else postcate += arraypostcate + ",";
                }
               alert(postcate);