简体   繁体   中英

Concat arrays with javascript and jquery

How I concat arrays if I have "i" number of arrays

var i = numberOfarrays;

so now I have to do something like that:

bigArray = row[0].concat(row[1]).concat(row[2])... ...concat(row[i]);

How I can concat i number of arrays?

You can do this :

var bigArray = Array.prototype.concat.apply([], row)

which can be reduced to

var bigArray = [].concat.apply([], row)

Fiddle : http://jsfiddle.net/qGVJe/

Try this,

Live Demo

var row = [[1,2],[3,4],[5,6]]
var bigArray=[];
for(i=0;i<row.length;i++)
   bigArray= bigArray.concat(row[i]);

Sample:

var a = ['1','2','3'];    
var b = ['4','5','6'];
var final_array = a.concat(b); 
//console.log shows final_array is now an an array with: ['1','2','3','4','5','6']

Apply:

bigArray=[];
for(i=0;i<small_array.length;i++){
    bigArray.concat(small_array[i]);
    }
or
 for(i=0;i<small_array.length;i++){
    bigArray.push(small_array[i]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM