javascript - Javascript 数组中没有数组

标签 javascript arrays loops error-handling

我是 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);
        }
     }
  }

最佳答案

下面的代码有效。

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

关于javascript - Javascript 数组中没有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39381851/

相关文章:

javascript - 如何在 vue.js 中使用属性选择器?

javascript - 函数仅返回首字母大写

C++ char数组赋值

java - 如何在矩阵中生成同心层

Javascript 根据 if 语句更改 array[i],然后在每次运行函数时将其连接起来

Javascript 超时循环将多行保存到 Parse.com

php - 某些字符串无法通过 GET 到达 PHP

javascript - Node js,使用带有 Q Promise 的 mongoose 不会调用拒绝函数

javascript - 如何保留对象的某些属性?

C - 初始化指针数组