【问题标题】:no array in an array at Javascript [duplicate]Javascript的数组中没有数组[重复]
【发布时间】:2017-01-15 20:24:08
【问题描述】:

我是 Javascript 新手,我有一个非常简单的问题。 我希望我的数组从 [1,2,3,[4,5,6,7,],9,10] 变为 [1,2,3,4,5,6,7,8,9,10] 但是当我尝试将它们循环到新数组时出现错误。 有更好的解决方案吗?

var theArray = [1,2,3,[4,5,6,7,],9,10] //I want this array become [1,2,3,4,5,6,7,8,9,10]

//when I try to do so it have error, below is my code
 //Test.isArray is a function I check is it an array

var i, j;
var IdLog =[]
for (j = 0; j < theArray.length; ++j) {
   if (!Test.isArray(tempID[j])) {
       IdLog.push(thArray[j]);
       console.log(IdLog);
        } else {
        for (k = 0; theArray[j].length; ++k) {
           IdLog.push(theArray[j][k]);
            console.log(IdLog);
        }
     }
  }

【问题讨论】:

  • 你没有在任何地方定义Test
  • theArray = [].concat.apply([], theArray)

标签: javascript arrays loops error-handling


【解决方案1】:

以下代码有效。

var theArray = [1,2,3,[4,5,6,7,],9,10];
IdLog = [].concat.apply([], theArray);
console.log(IdLog);

var theArray = [1,2,3,[4,5,6,7,],9,10];
IdLog=theArray.join(",").split(",");
console.log(IdLog);

Demo

【讨论】:

  • 非常感谢,我使用了 1 个代码,它可以工作! :D
猜你喜欢
  • 1970-01-01
  • 2020-04-17
  • 2016-09-14
  • 2021-03-31
  • 2019-04-18
  • 2022-01-13
  • 1970-01-01
  • 2012-07-23
  • 2013-05-29
相关资源
最近更新 更多