0

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?

2
  • do you want to store all elemnents in a main array?? Commented Jul 30, 2013 at 13:28
  • yes to stor all arrays ibto one big Commented Jul 30, 2013 at 13:31

3 Answers 3

4

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/

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I've just deleted my post with exactly the same solution, you posted it 50 secs earlier :)
2

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]);

2 Comments

You should try... concat doesn't work like you think it works.
It still seems unnecessary to use concat like this. What you're after is push. concat shouldn't need to be used like this in an loop, especially because it creates/returns a new array every concat call
0

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]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.