Skip to main content
added 56 characters in body
Source Link
Code Maniac
  • 37.9k
  • 5
  • 44
  • 65

You can use a simple for loop and a temp variable and push values on every second index to final output

var arr = [`A`,`C`,`C`,`D`,`C`,`B`] let op = [] let temp = [] for(let i = 0; i < arr.length; i++){ temp.push(arr[i]) if( i % 2 == 1 ){ op.push(temp) temp = [] } } if( temp.length > 0 ){ op.push(temp) }  console.log(op)

You can use a simple for loop and a temp variable and push values on every second index to final output

var arr = [`A`,`C`,`C`,`D`,`C`,`B`] let op = [] let temp = [] for(let i = 0; i < arr.length; i++){ temp.push(arr[i]) if( i % 2 == 1 ){ op.push(temp) temp = [] } } console.log(op)

You can use a simple for loop and a temp variable and push values on every second index to final output

var arr = [`A`,`C`,`C`,`D`,`C`,`B`] let op = [] let temp = [] for(let i = 0; i < arr.length; i++){ temp.push(arr[i]) if( i % 2 == 1 ){ op.push(temp) temp = [] } } if( temp.length > 0 ){ op.push(temp) }  console.log(op)

Source Link
Code Maniac
  • 37.9k
  • 5
  • 44
  • 65

You can use a simple for loop and a temp variable and push values on every second index to final output

var arr = [`A`,`C`,`C`,`D`,`C`,`B`] let op = [] let temp = [] for(let i = 0; i < arr.length; i++){ temp.push(arr[i]) if( i % 2 == 1 ){ op.push(temp) temp = [] } } console.log(op)