0
const array = [1...7]

so i have this array as u can see the length is 7, so i want the result like this

[1, 2, 3, 4, 5] and [6, 7]


same for the array below

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

the length is 11, therefore the result i want should be

[1, 2, 3, 4, 5] and [7, 8, 9, 10] and [11]

why 11 only? because i want to seperate array lengths by 5

Vcut Dev
  • 13
  • 2

1 Answers1

-1

I had written an splitter function that does the job:

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

function splitter ( arr, length = 5 ) {
  let result = [];
  let intermediateArray = [];
  arr.reverse().map( (el,index) => {
    intermediateArray.push(el);
   
    if (index % length === 0) {
      result.push(intermediateArray.reverse());
      intermediateArray = []
    }
  })
  return result.reverse();
}

console.log(splitter(array))
Mechanic
  • 5,015
  • 4
  • 15
  • 38