javascript - Concat sequential 2 elements of an array as an array in new array -


suppose if have array [1,2,3,4,5,6], want new array [[1,2],[3,4],[5,6]]. don't want use loop.i want solve using map-reduce or using best practice not getting how use map-reduce. please help.

you use remainder operator % checking. if zero, push last inserted array otherwise result new array.

var array = [1, 2, 3, 4, 5, 6],      result = array.reduce(function (r, a, i) {          % 2 ? r[r.length - 1].push(a) : r.push([a]);          return r;      }, []);    console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -