1

I am new to Javascript, I have a very simple question. I want my array become [1,2,3,4,5,6,7,8,9,10] from [1,2,3,[4,5,6,7,],9,10] But I get error while I try to loop them to a new array. Any better solution?

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);
        }
     }
  }
Noel Chung
  • 75
  • 8

1 Answers1

0

Below code works.

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

or

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

Demo

Naga
  • 2,160
  • 3
  • 14
  • 21