Print two arrays side by side in a multidimensional array Javascript -
this code:
function chunk(arr, size) { answer = [['']['']]; firsthalf = arr.slice(0,size); secondhalf = arr.slice(size, arr.length); return firsthalf; } chunk(['a', 'b', 'c', 'd'], 2);
my goal print 2 cut arrays side side. i'm looking result this:
[[ 'a', 'b' ],[ 'c', 'd' ]]
i know need push 2 seperate arrays somehow, can't seem work.
here 1 way create data structure you're looking for. modified function returns array 2 split arrays in it.
function chunk(arr, size) { var firsthalf = arr.slice(0,size); var secondhalf = arr.slice(size, arr.length); return [firsthalf, secondhalf]; } var out = chunk(['a', 'b', 'c', 'd'], 2); console.log(out);
Comments
Post a Comment