0

How can I concat 'n' number of arrays into a single array using jQuery.

     a1 = [a,b,c,d,f];
     a2 = [h,g,f,r];
     ...............
     an = [r,e,c,g,s,g];

and I need to get like

     A = [a,b,c,d,f,h,g,f,r,.....,r,e,c,g,s,g];

Please help. Thank you in advance for all replay

Mansoor
  • 130
  • 1
  • 11

8 Answers8

4

You're looking for array concat method

jsBin demo

var arr1 = ["a", "b"];
var arr2 = ["c", "d"];

var arr3 = arr1.concat(arr2);

console.log(arr3);  // ["a", "b", "c", "d"]

You can find more info here:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1
var A=a1.concat(a2,a3,a4,....,an);
polin
  • 2,745
  • 2
  • 15
  • 20
  • I don't know the number of arrays it is a variable – Mansoor Nov 27 '12 at 11:19
  • The way mentioned is the right way. Now the question is how are you getting the arrays? – polin Nov 27 '12 at 11:21
  • I've got the answer right here: http://stackoverflow.com/a/13582972/1835379 you can check if a variable exists, and loop though them, provided they have consistent naming. – Cerbrus Nov 27 '12 at 11:22
1

Assuming your arrays are all named a<x>, and global variables, you can loop through them on the window object as follows:

var output = [];
for(var i = 1; window['a'+i]; i++){
    output = output.concat(window['a'+i]);
}

Working example

Output will then, at the end of the loop, be a concatenation of all the arrays.

window['a'+i] will refer to the a1 - aX arrays, since global variables are properties of the window object. As such, these are all identical:

var output = "Some content"

console.log(output);
console.log(window.output);
console.log(window['output']);
// These will all return "Some content"

You can also manually concatenate them like this if you know how many you have:

output = a1.concat(a2,a3,a<x>...);
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Tested and working in Chrome. You can't have gaps in the array numbering, however. (Having arrays like: `a1, a2, a3, a5` will not let this concatenate `a5`) – Cerbrus Nov 27 '12 at 11:24
0
Array.prototype.concat([1,2,3],[4,5])

or

[1,2,3].concat([4,5])
Jashwant
  • 28,410
  • 16
  • 70
  • 105
Itay Kinnrot
  • 721
  • 5
  • 11
0

Try with $.merge; example:

var new = $.merge( [0,1,2], [2,3,4] ); // new = [0,1,2,2,3,4]
ilCrosta
  • 95
  • 1
  • 5
  • jQuery.merge merges the contents of two arrays together into the first array. The concat() method does not change the existing arrays, but returns a new array. – Corneliu Nov 27 '12 at 11:18
  • Don't use jQuery for something that js has native functionality for. Also, don't use `new` as variable name, it's a reserved keyword. – Cerbrus Nov 27 '12 at 11:19
  • Sure sure, i write "new" by mistake. I never use "concat", i will try in future. Thanks! – ilCrosta Nov 27 '12 at 11:24
0

Use concat() method.

var a1 = ["a","b","c","d","f"]; 
var a2 = ["h","g","f","r"]

var a3 = a1.concat(a2);

console.log(a3);  // Outputs: ["a", "b", "c", "d", "f", "h", "g", "f", "r"]
Jashwant
  • 28,410
  • 16
  • 70
  • 105
0

You can use standart function concat.

AChudov
  • 214
  • 1
  • 4
-1

Something similar to:

for(a2...an){
    a1 = $.merge(a1,next);
}
Chexpir
  • 1,876
  • 18
  • 33