6

I have 2 arrays, say arr1=[A,B,C,D];
and arr2= [a,b,c,d]; I want to create a third array by combining these 2 in the following way:

arr3= [A,a,B,b,C,c,D,d]; 

How can I achieve this using jquery? Please help!

2
  • 1
    Define exactly how you're ordering the elements in the final array. Alphabetical, or do you just want to alternate between arr1 and arr2? Commented May 15, 2014 at 13:59
  • Just want to alternate between arr1 and arr2. In exact way as shown in the question. Commented May 15, 2014 at 14:03

6 Answers 6

21

Try to use jquery's $.merge(array1,array2) function,

var arr3 = $.merge( arr1, arr2 )

DEMO

For the format you have asked,

var arr1=['A','B','C','D'];
var arr2=['a','b','c','d'];

var arr3 = [];

for(var i=0,len=arr1.length;i<len;i++){
    arr3[arr3.length] = arr1[i];
    arr3[arr3.length] = arr2[i];
}

alert(arr3);

DEMO I

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

5 Comments

This won't put it in the order shown in the question.
Note that, according to the docs, arr1 will be altered by this operation.
@RocketHazmat Yeah, that's true. but op just asked how to combine two arrays.
He did ask for it to be in a certain format, though.
@RocketHazmat answered. :)
2

If you just want to alternate elements between the two arrays, and that they're always equal in length:

arr3 = [];
for (var i=0,j=arr1.length; i<j; i++) {
    arr3.push(arr1[i]);
    arr3.push(arr2[i]);
}

Comments

1

It can be done in one string by using merge function. The new array will be returned without altering merged arrays:

var arr3 = $.merge($.merge([], arr1), arr2);

Comments

1

you can use $.merge function as

$(document).ready(function(){
  var arr1=['a','b'];
  var arr2=['c','d'];
  console.log($.merge(arr1,arr2));  
});

http://jsfiddle.net/xpvt214o/850274/

Comments

0

This will interleave two arrays of any length, and return the result. No jquery required.

function interleave(a,b) {
    c=[];
    tot=a.length+b.length;
    while (c.length<tot) {
        if (aa=a.shift()) {
            c.push(aa);
        }
        if (bb=b.shift()) {
            c.push(bb);
        }
    }
    return c;
}

If you want to merge two arrays and sort alphabetically, try this (also no jquery)

arr3=arr1.concat(arr2).sort()

Comments

0

You can interleave two arrays using the recursive function below. This method will work whether or not the arrays are the same length:

var z = (a, b) => a.length ? [a[0], ...z(b, a.slice(1))] : b;

var arr1 = ["A", "B", "C", "D"];
var arr2 = ["a", "b", "c", "d"];

var arr3 = z(arr1, arr2);
console.log(JSON.stringify(arr3));

or for any number of arrays, use this:

var z = (a = [], ...b) => 
    b.length ? a.length ? [a[0], ...z(...b, a.slice(1))] : z(...b) : a;

var array1 = [1, 2];
var array2 = '♦♡♣♤♥♢';
var array3 = ['A', 'B', 'C'];
var array4 = ['😊', '😔', '��'];
var array5 = [null, NaN, undefined];

var stringify = (o) => JSON.stringify(o, (k, v) => v === undefined ? '__undefined__' : v !== v ? '__NaN__' : v).replace(/"__undefined__"/g, 'undefined').replace(/"__NaN__"/g, 'NaN');

console.log(stringify(z()));
console.log(stringify(z(array1)));
console.log(stringify(z(array1, array2)));
console.log(stringify(z(array1, array2, array3)));
console.log(stringify(z(array1, array2, array3, array4)));
console.log(stringify(z(array1, array2, array3, array4, array5)));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.