我是 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);
关于javascript - Javascript 数组中没有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39381851/