Combine arrays in JavaScript

I have several sets that I am trying to combine in a specific order. For example, let's say I have three arrays:

var arrayOne = [1a, 1b, 1c];
var arrayTwo = [2a, 2b, 2c];
var arrayThree [3a, 3b, 3c];

how do i get something like this?

var combinedArray = [1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, 3c] 

EDIT

Well, I'm going to add a little more to my story here, since I have excellent answers and answers from everyone. Maybe it will be clearer. I have a SharePoint list from which I get all my information. I use SPServices to get my data from a list. I also get the version history of each item in the list and putting them in arrays, so I can pull the data from the SPServices cycle and use it outside of the SharePoint stuff, so I can try to order and display it the way I want. (Please do not move this to SharePoint ). This should all happen at runtime. So, here is the first part. I declare global variables for each of my list items:

var proModified6 = new Array();
var proHistory = new Array();
var assignedTo = new Array();
var startDate = new Array();
var endDate = new Array();
var status = new Array();
var project = new Array();
var history = new Array();


var listItems = new Array();


var regex = new RegExp("");

var i = 0;

Then I populate arrays with SharePoint list information (I'm not going to put them all, but everyone has a call like this)

$().SPServices({
  operation: "GetVersionCollection",
      async: false,
      webURL: "http://devchrisl01/test",
      strlistID: "NewProjects",
      strlistItemID: proID[i],
      strFieldName: "Title",         

      completefunc: function (xdata, Status) {

        $(xdata.responseText).find("Version").each(function() {

       //alert(xdata.responseXML.xml);

       var xitem = $(this);
            var ID = xitem.attr('ID');
            var Project = xitem.attr('Title');
            var Modified = xitem.attr('Modified').split('T')[0];
            var ModifiedTime = xitem.attr('Modified').substring(11, 19);
           //var modifiedUl = "<td><b>" + Modified + " " + ModifiedTime + "</b></td>";
  //$('#versionList'+i+'').append(modifiedUl);

                  project.push(Project);
                  proModified2.push(Modified + ModifiedTime)


    // just looking at my data here not really part of my problem       
var data = "<tr><td><b>" + Modified + " " + ModifiedTime + "</b></td><td>" + Project + "</td></tr>";


$('#versionList'+i+'').append(data);

        });  

      }

});

. . SPServices, , . : , . , . (PAIN IN THE ASS ), :

var modifiedDate = [proModified1, proModified2, proModified3, proModified4, proModified5, proModified6];
var projectHistory = [history];
var projectTitle = [project];
var projectAssignedTo = [assignedTo];
var projectStartDate = [startDate];
var projectEndDate = [endDate];
var projectStatus = [status];

. . , . : 0

, ? , lol.

+3
5

: , , -1, 0 1.

,

var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];

var combinedArray = arrayOne
    .concat(arrayTwo, arrayThree) //Concatenate the array.. can add any number of arrays
    .sort(function(a, b) { //Custom comparator for your data
       var a1 = a.charAt(1);
       var b1 = b.charAt(1);

       if (a1 == b1) return 0;
       if (a1 < b1) return -1;
       return 1;
    });

. . .

DEMO

+3

:

var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];

var newArray = [];
for (var i = 0, len = arrayOne.length; i < len; i++) {
    newArray.push(arrayOne[i], arrayTwo[i], arrayThree[i]);
}

console.log(newArray); //1a,2a,3a,1b,2b,3b,1c,2c,3c

, , :

var arrayOne = ["1a", "1b", "1c", "1d"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b"];

var newArray = [];
var len = Math.max(arrayOne.length, arrayTwo.length, arrayThree.length);

for (var i = 0; i < len; i++) {
    if (i in arrayOne) {
        newArray.push(arrayOne[i]);
    }
    if (i in arrayTwo) {
        newArray.push(arrayTwo[i]);
    }
    if (i in arrayThree) {
        newArray.push(arrayThree[i]);
    }
}

console.log(newArray); //1a,2a,3a,1b,2b,3b,1c,2c,1d

i in arr ( ) , falsish 0,"",false,null,undefined,NaN . ( undefined )

+2

, zip, python. javascript, , :

zip = function() {
    var 
        args = [].slice.call(arguments, 0),
        len = Math.max.apply(Math, args.map(function(x) { return x.length })),
        out = [];
    for (var i = 0; i < len; i++)
        out.push(args.map(function(x) { return x[i] }));
    return out;
}

var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c"];
var arrayThree = ["3a", "3b", "3c"];

zipped = zip(arrayOne, arrayTwo, arrayThree);

:

[["1a", "2a", "3a"], ["1b", "2b", "3b"], ["1c", "2c", "3c"]]

:

 flat = [].concat.apply([], zipped)
+2

JS sort() . , /[0-9][a-z]/, :

//concat arrays first, of course
function specialSort(a,b)
{
    if(a[1] < b[1])
    {
        return -1;
    }
    if(a[1] > b[1])
    {
        return 1;
    }
    return 0;
}
//using wsanville expample: combinedArray = ["1a", "1b", "1c", "2a", "2b", "2c", "3a", "3b", "3c"]
combinedArray = combinedArray.sort(specialSort);
//result: combinedArray = ["1a", 2a", "3a", "1b", "2b", "3b", "1c", "2c", "3c"]

, ...

+1

:

var arrayOne = ["1a", "1b", "1c"];
var arrayTwo = ["2a", "2b", "2c", "2d", "2e"];
var arrayThree = ["3a", "3b", "3c", "3d"];
var combinedArray = [];

for (var i = 0, j = Math.max(arrayOne.length, arrayTwo.length, arrayThree.length); i < j; i++) {
    if (arrayOne.length) combinedArray.push(arrayOne.shift());
    if (arrayTwo.length) combinedArray.push(arrayTwo.shift());
    if (arrayThree.length) combinedArray.push(arrayThree.shift());
}

alert(combinedArray.join(", "));​

This should probably work for variable length arrays. Demo is here .

+1
source

All Articles