【发布时间】: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