1

I have an array and i have to split it into given number and last splited array should accommodate remaining elements if (remaining array size < given_no/2)

for example

var arr = [1,2,3,4,5,6,7,8,9,10,11]

if given_no 2 = Math.round(arr.length/2)= chunk_size 6 = [[1,2,3,4,5,6],[6,7,8,9,10]] 
if given_no 3 = Math.round(arr.length/3)= chunk_size 4 = [[1,2,3,4],[5,6,7,8],[9,10,11]]
if given_no 4 = Math.round(arr.length/4)= chunk_size 3 = [[1,2,3],[4,5,6],[7,8,9],[10,11]]
3
  • give_number is only till 4 or is it dynamic ? Commented Jul 16, 2020 at 6:31
  • Math.round(11/2) == 6, not 5. Did you mean Math.floor(11/2)? - If so, Math.floor(11/3) == 3 and Math.floor(11/4) == 2... Also, I'm unclear about "if (remaining array size < given_no/2)". Commented Jul 16, 2020 at 7:15
  • @SijuSamson..its dynamic (given_no) Commented Jul 16, 2020 at 7:25

3 Answers 3

2

First of all Math.round(arr.length/2) will return 6 not 5. Please see below code. I have created splitArr function which will return split array based on given number.

var arr = [1,2,3,4,5,6,7,8,9,10,11];
console.log('Given 2: ' + JSON.stringify(splitArr(arr, 2))); // given 2
console.log('Given 3: ' + JSON.stringify(splitArr(arr, 3))); // given 3
console.log('Given 4: ' + JSON.stringify(splitArr(arr, 4))); // given 4
console.log('Given 5: ' + JSON.stringify(splitArr(arr, 5))); // given 5
console.log('Given 6: ' + JSON.stringify(splitArr(arr, 6))); // given 6

    function splitArr(arr, given) {
      var arrSize = arr.length
      var split = Math.round(arrSize/given);
      var returnArr = [];
      var loopCount;
      for(i = 0; i < given; i++){
        loopCount = (i*split)+split;
        loopCount = loopCount > arrSize ? arrSize : loopCount;
        
        var innerArr = arr.slice((i*split), loopCount)

        returnArr.push(innerArr);
      }

      if(loopCount < arr.length){
         innerArr.push(...arr.slice(loopCount, arrSize));
      }
      
      return returnArr;
    }

Sign up to request clarification or add additional context in comments.

6 Comments

It fails for => splitArr(5); // given 5 => Given 5: [[1,2],[3,4],[5,6],[7,8],[9,10]]...where is element '11'
@NRaj please verify edited answer and upvote if it works.
How about performance , If array size in beween 100k to 500k?
Improved my answer please verify that. Removed 2 inner for loops this will improve performance.
For give = 5, you seem to have the further nesting: Given 5: [[1,2],[3,4],[5,6],[7,8],[9,10,[11]]]. You have [9, 10, [11]]
|
1

You can use Math.round() and array.slice().

var splitArray = (arr, num) => {
    let result = [];
    let size   = Math.round(arr.length/num); 
    for (let i=0; i<num; i++) {
        let end = i === num-1 ? arr.length : (i+1) * size;
        result.push(arr.slice(i*size, end));
    }
    return result;  
} 

var arr = [1,2,3,4,5,6,7,8,9,10,11];
console.log(JSON.stringify(splitArray(arr, 2)));
console.log(JSON.stringify(splitArray(arr, 3)));
console.log(JSON.stringify(splitArray(arr, 4)));
console.log(JSON.stringify(splitArray(arr, 5)));

3 Comments

this is not working with console.log(JSON.stringify(splitArray(arr, 5)));
Yes, this also fails for console.log(JSON.stringify(splitArray(arr, 5)));
OK Math.round() it is. Fixed.
0
 function splitintochunks(arr,l){
    let result=[];
    while(arr.length>0){
    result.push(arr.splice(0,l));
    }
    return result;
}
//EXAMPLE: call in the below way spltIntoChunks([1,2,3,4,5,6,7,8,9,10,11],5);

2 Comments

This will not work, please test it once, and then give appropriate result.
@SijuSamson, got my mistake , thanks :) for adding the comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.