1

I have the following two arrays:

var arr1 = [1,2,4,5];
var arr2 = [];

How would I go about to split it when there is no consecutive value?

In this example, it should be split into: [ [1,2] and [4,5].

These two arrays should be stored in arr2.

Example 2 :

var arr3 = [1,2,3,5,6,7,8,9,10,11,13]

Result : [[1,2,3], [5,6,7,8,9,10], [11], [13]]

Karthik VU
  • 561
  • 5
  • 17
  • And then what? How would you want that to be stored? – Cerbrus Sep 19 '16 at 11:57
  • Ah I'm sorry. I wasn't clear enough - i see now. I would store it in an array which holds these arrays. I`ll write that in the main question now :) – Konstantin Hadzhiev Sep 19 '16 at 11:58
  • 1
    This conversation might help you: [Split array into chunks - StackOverflow](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – op_ Sep 19 '16 at 12:01

5 Answers5

7

You could use Array#reduce and check if the element is consecutive. Then append to the last array, otherwise push a new array to the result set.

var array = [1, 2, 4, 5],
    result = array.reduce(function (r, a, i, aa) {
        if (!i || aa[i - 1] + 1 !== a) {
            r.push([a]);
        } else {
            r[r.length - 1].push(a);
        }
        return r;
    }, []);

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Here is a way to do it :

var arr1 = [1,2,4,5];

var slices = [];
var sliceNb = 0;
arr1.forEach(function(v, k, arr){
 if(!slices[sliceNb]){
  slices[sliceNb] = [];
 }
 slices[sliceNb].push(v);

 if(arr[k+1] && arr[k+1] > v+1){
  sliceNb++;
 }
});

console.log(slices);
vincenth
  • 1,732
  • 5
  • 19
  • 27
0

try this:

arr1 = [1,2,3,4]
new_arr = [arr1.slice(0, (arr1.length/2) ), arr1.slice(arr1.length/2 +1, arr1.length)]
new_arr
//[[1,2],[3,4]]
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Sep 19 '16 at 13:56
0

You can figure out the chunk size my dividing the size of the array by the number of partitions. Then you can simply slice the array.

var arr1 = [1, 2, 4, 5];
var arr2 = partitionArray(arr1, 2); // [[1, 2], [4, 5]]

document.body.innerHTML = '<pre>arr2 = ' + JSON.stringify(arr2) + '</pre>';

/**
 * Partitions an array into chunks.
 * @param  {Array}  arr - The array to partition.
 * @param  {int}    n   - The number of partitions. 
 * @return {Array} Returns a partitioned array.
 */
function partitionArray(arr, n) {
  var chunkSize = Math.max(arr.length / n, 1);
  return [].concat.apply([], arr.map(function(item, i) {
    return i % chunkSize ? [] : [arr.slice(i, i + chunkSize)];
  }));
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

You might do as follows;

var arr = [1,2,4,5,6,111,111,112],
    res = arr.reduce((p,c,i,a) => c === a[i-1]+1 ? (p[p.length-1].push(c),p) 
                                                 : p.concat([[c]]),[]);
console.log(res);
Redu
  • 25,060
  • 6
  • 56
  • 76