Skip to main content
Known Participant
July 13, 2018
Question

Splitting the array into pairs.

  • July 13, 2018
  • 2 replies
  • 256 views

Hi,

I have array with numbers and i want to create pairs from this table, like this:

var doc = app.activeDocument;

var table = ["1", "2", "3", "4", "5", "6", "7", "8", "9","10"]; // the table will be variable so the number of pairs will be variable too there can be 2-4 numbers and  it can be 30.

//some magic function or loop

var pairs = {

           1. pair = ["1", "2"]

           2. pair = ["2", "3"]

           3. pair = ["3", "4"]

           4. pair = ["4", "5"] etc.

} ;

This topic has been closed for replies.

2 replies

Disposition_Dev
Legend
July 13, 2018

function splitArrayIntoPairs()

{

    var table = ["1","2","3","4","5","6","7","8","9","10"];

    var pairedArrays = [];

    for(var x=0,len=table.length;x<len;x+=2)

    {

        pairedArrays.push([table,table[x+1]]);

    }

}

splitArrayIntoPairs();

Known Participant
July 13, 2018

I think I solved the problem:

var doc = app.activeDocument;

var table = ["1", "2", "3","4","5", "6"];

function splitTable(){

   

   var chunks =  table.length -1

   var t =  table.length - 1;

   var d=  table.length;

   var tab3 = table.slice(0,t)

   var tab4 = table.slice(1,d)

   var newTable = [];

         for (var i=0 ; i<chunks; i++) {

   

                var sum = [ [ tab3  ] + " - "  +  [ tab4 ]  ]; 

               newTable.push(sum + "\n");

}

return newTable;

}

alert( splitTable() ) ;