-3

How can I split array into chunks with some special algorithm? E.g. I need to shorten array to the size of 10 elements. If I have array of 11 elements, I want two next standing elements get merged. If I have array of 13 elements, I want three elements merged. And so on. Is there any solution?

Sample #1
var test = ['1','2','3','4','5','6','7','8','9','10','11'];
Need result = [['1'],['2'],['3'],['4'],['5|6'],['7'],['8'],['9'],['10'],['11']]

Sample #2
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13'];
Need result = [['1|2'],['3'],['4'],['5'],['6'],['7|8'],['9'],['10'],['11'],['12|13']]

Thank you in advance.

Mike B
  • 2,756
  • 2
  • 16
  • 28
Alex F
  • 183
  • 2
  • 14
  • First - show some effort. Second - how do you detect which elements are merged? – Mike B Sep 17 '16 at 07:14
  • @Mikelis Baltruks I don't need to detect. Merging of random element with next standing one to shorten array is enough – Alex F Sep 17 '16 at 07:17
  • but why in first example merge is in the middle, but in first it is at both ends? You want just any? – Mike B Sep 17 '16 at 07:19
  • anyway - you will not get any code from anyone if you don't try yourself. basic idea is, you loop through the array and put elements into 2d array with each element forming a new (1d) array. and to merge - just get the difference (11 needed, you have 15 elements. diff = 4) and take that many first pairs and merge. rest just go in the normal way – Mike B Sep 17 '16 at 07:25
  • Yes, in first sample the random key was 4 and it got merged with 5. In second element random key was 0(merged with 1), next random key was 6(merged with key 7) and the last random key was 12(merged with key 13). I just want to shorten array. – Alex F Sep 17 '16 at 07:27
  • Mikelis, I don't need code, I need an algorithm. I have one, but I don't like it. Don't overestimate your knowledge mate. – Alex F Sep 17 '16 at 07:28
  • Looping isn't a solution. If I loop the merged elements will be the first values, while I need random – Alex F Sep 17 '16 at 07:29
  • *Don't overestimate your knowledge mate.* And with that you want help from me? – Mike B Sep 17 '16 at 08:02

3 Answers3

1

A - Find the difference and create thus many random numbers for merge and put in array
B - loop through initial numbers array.
B1 - if iterator number is in the merge number array (with indexOf), you merge it with the next one and increase iterator (to skip next one as it is merged and already in results array)
B1 example:

int mergers[] = [2, 7, 10]
//in loop when i=2
if (mergers.indexOf(i)>-1) { //true
   String newVal = array[i]+"|"+array[i+1]; //will merge 2 and 3 to "2|3"
   i++; //adds 1, so i=3. next loop is with i=4
}

C - put new value in results array

Mike B
  • 2,756
  • 2
  • 16
  • 28
1

You can try this code

jQuery(document).ready(function(){ 
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'];  


        var arrays = [];

        var checkLength =  test.length;

        var getFirstSet = test.slice(0,10);
        var getOthers = test.slice(10,checkLength);

        $.each( getFirstSet, function( key,value ) {
              if(key in getOthers){
                values = value +'|'+ getOthers[key]; 
                arrays.push(values);
              }else{
                arrays.push(value);            
              }
        });


        console.log(arrays);
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Parithiban
  • 1,656
  • 11
  • 16
1

The following code most probably does what you want.

function condense(a){
  var  source = a.slice(),
          len = a.length,
  excessCount = (len - 10) % 10,
         step = excessCount - 1 ? Math.floor(10/(excessCount-1)) : 0,
    groupSize = Math.floor(len / 10),
     template = Array(10).fill()
                         .map((_,i) => step ? i%step === 0 ? groupSize + 1
                                                           : i === 9 ? groupSize + 1
                                                                     : groupSize
                                            : i === 4 ? groupSize + 1
                                                      : groupSize);
  return template.map(e => source.splice(0,e)
                                 .reduce((p,c) => p + "|" + c));
}

var test1 = ['1','2','3','4','5','6','7','8','9','10','11'],
    test2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
console.log(condense(test1));
console.log(condense(test2));
Redu
  • 25,060
  • 6
  • 56
  • 76