gpt4 book ai didi

javascript - Javascript 数组中没有数组

转载 作者:行者123 更新时间:2023-11-28 05:35:42 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com