Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Arrays in Order JavaScript

I have a few arrrays which I am trying to combine in a certain order. For example, lets say I have three arrays:

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

how would I get something like this?

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

EDIT

Ok, I am going to add a little more to my story here since I have gotten some great responses and answers from everyone. Maybe this will be clearer. I have a SharePoint list which I am getting all of my information from. I am using SPServices to get my data from the list. I am also getting the version history of each list item and placing them into arrays so I can pull the data out of the SPServices loop and use it outside of the SharePoint stuff so I can try to order and display it as I want. (Please do not move this to SharePoint ). This is all going to have to happen at runtime. So here is the first part. I am declaring global variables for each one 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 am filling the arrays with the SharePoint list info ( I am not going to put them all but each has a call like this one)

$().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);

        });  

      }

});

After is where my question has come into play. I am getting all of my data back I need. I have not found a better way to store all of the list information and pull it out of the SPServices besides using an array for each. The kicker is I am not going to know how many arrays there are going to be or how long. Eventually this is going to be dynamic. (PAIN IN THE ASS ) so here are the arrays:

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

Each one is going to be different. There is not going to be the same amount of them either for each user. I have just made my list static to build on first. List dynamics will be another question later :0

Is there going to be a way for me to do with these arrays like I asked in my simple example? See why I went simple first lol.

like image 334
lazoDev Avatar asked Jan 18 '26 21:01

lazoDev


1 Answers

Edit: Updated comparator as Salmon pointed out that it should return -1, 0 or 1.

Try below,

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

Note: The custom comparator function is very specific to your data. Update the function as you need.

DEMO

like image 71
Selvakumar Arumugam Avatar answered Jan 21 '26 11:01

Selvakumar Arumugam