Use JavaScript or Lodash to create multi dimensional array












0















I want to create a multi-dimensional array like this using Lodash or vanilla JS:



[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
etc
]


This is a simplistic example since I want this pattern to continue up to 1,000,000 but for demonstration 1 to 20 is fine.



Any ideas? I've tried _.range(20) so far but I need this Array to be multi-dimensional. Thanks










share|improve this question























  • What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

    – Phil
    Nov 20 '18 at 23:54






  • 1





    Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

    – zerkms
    Nov 20 '18 at 23:58
















0















I want to create a multi-dimensional array like this using Lodash or vanilla JS:



[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
etc
]


This is a simplistic example since I want this pattern to continue up to 1,000,000 but for demonstration 1 to 20 is fine.



Any ideas? I've tried _.range(20) so far but I need this Array to be multi-dimensional. Thanks










share|improve this question























  • What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

    – Phil
    Nov 20 '18 at 23:54






  • 1





    Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

    – zerkms
    Nov 20 '18 at 23:58














0












0








0








I want to create a multi-dimensional array like this using Lodash or vanilla JS:



[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
etc
]


This is a simplistic example since I want this pattern to continue up to 1,000,000 but for demonstration 1 to 20 is fine.



Any ideas? I've tried _.range(20) so far but I need this Array to be multi-dimensional. Thanks










share|improve this question














I want to create a multi-dimensional array like this using Lodash or vanilla JS:



[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
etc
]


This is a simplistic example since I want this pattern to continue up to 1,000,000 but for demonstration 1 to 20 is fine.



Any ideas? I've tried _.range(20) so far but I need this Array to be multi-dimensional. Thanks







javascript lodash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 23:49









danday74danday74

18.8k1491130




18.8k1491130













  • What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

    – Phil
    Nov 20 '18 at 23:54






  • 1





    Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

    – zerkms
    Nov 20 '18 at 23:58



















  • What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

    – Phil
    Nov 20 '18 at 23:54






  • 1





    Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

    – zerkms
    Nov 20 '18 at 23:58

















What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

– Phil
Nov 20 '18 at 23:54





What happened when you tried _.range(20)? Perhaps you'd then be interested in _.chunk()

– Phil
Nov 20 '18 at 23:54




1




1





Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

– zerkms
Nov 20 '18 at 23:58





Pure JS: Array.from(new Array(30)).map((_, i) => i + 1).reduce((acc, v) => v % 10 === 1 ? [...acc, [v]] : (acc[Math.floor((v - 1) / 10)].push(v), acc), )

– zerkms
Nov 20 '18 at 23:58












4 Answers
4






active

oldest

votes


















3














With lodash you can use chunk:






const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








share|improve this answer





















  • 1





    Just a minor correction... OP's arrays start at 1

    – Phil
    Nov 20 '18 at 23:57











  • @Phil thanks, corrected.

    – slider
    Nov 20 '18 at 23:58






  • 1





    Upvote! First thing I though about when I read the question and then realized you already wrote it :)

    – Akrion
    Nov 21 '18 at 0:48



















5














Using nested native Array#from()






const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)








share|improve this answer





















  • 2





    That's really really really really cool

    – zerkms
    Nov 21 '18 at 0:04






  • 1





    Array#from() 👈 spot the Java dev ;-)

    – Phil
    Nov 21 '18 at 0:05











  • I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

    – Phil
    Nov 21 '18 at 0:07











  • @Phil right...I assumed even 10's based on 20 and 1,000,000

    – charlietfl
    Nov 21 '18 at 0:09






  • 2





    It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

    – Mark Meyer
    Nov 21 '18 at 0:16





















3














With vanilla javascript:






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





For anyone who's interested, I time clocked these answers and this is the fastest. Using 1 million entries, this clocks at .100 seconds. Lodash chunk clocks close behind at .110s. Array.from lags behind at .254s.






share|improve this answer


























  • Old school cool :)

    – slider
    Nov 21 '18 at 0:15






  • 1





    @Phil hah, fixed... and time-clocked

    – Conner
    Nov 21 '18 at 0:33



















3














Looking at the ES6 Array.from documentation you can see a range generator function:




const range = (start, stop, step) => Array.from({ length: (stop -
start) / step }, (_, i) => start + (i * step));



So with that in mind you could generate the range via:



range(1, 21, 1)  // account for the 0 with the 21 otherwise you get 1-19


Then all that is missing is the chunk function:






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





The chunkBy uses Array.reduce & the % operator to chunk the data.






share|improve this answer





















  • 1





    And you wrote a mini lodash :D

    – slider
    Nov 21 '18 at 0:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403334%2fuse-javascript-or-lodash-to-create-multi-dimensional-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














With lodash you can use chunk:






const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








share|improve this answer





















  • 1





    Just a minor correction... OP's arrays start at 1

    – Phil
    Nov 20 '18 at 23:57











  • @Phil thanks, corrected.

    – slider
    Nov 20 '18 at 23:58






  • 1





    Upvote! First thing I though about when I read the question and then realized you already wrote it :)

    – Akrion
    Nov 21 '18 at 0:48
















3














With lodash you can use chunk:






const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








share|improve this answer





















  • 1





    Just a minor correction... OP's arrays start at 1

    – Phil
    Nov 20 '18 at 23:57











  • @Phil thanks, corrected.

    – slider
    Nov 20 '18 at 23:58






  • 1





    Upvote! First thing I though about when I read the question and then realized you already wrote it :)

    – Akrion
    Nov 21 '18 at 0:48














3












3








3







With lodash you can use chunk:






const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








share|improve this answer















With lodash you can use chunk:






const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>








const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>





const result = _.chunk(_.range(1, 21), 10);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 23:58

























answered Nov 20 '18 at 23:56









sliderslider

8,25811130




8,25811130








  • 1





    Just a minor correction... OP's arrays start at 1

    – Phil
    Nov 20 '18 at 23:57











  • @Phil thanks, corrected.

    – slider
    Nov 20 '18 at 23:58






  • 1





    Upvote! First thing I though about when I read the question and then realized you already wrote it :)

    – Akrion
    Nov 21 '18 at 0:48














  • 1





    Just a minor correction... OP's arrays start at 1

    – Phil
    Nov 20 '18 at 23:57











  • @Phil thanks, corrected.

    – slider
    Nov 20 '18 at 23:58






  • 1





    Upvote! First thing I though about when I read the question and then realized you already wrote it :)

    – Akrion
    Nov 21 '18 at 0:48








1




1





Just a minor correction... OP's arrays start at 1

– Phil
Nov 20 '18 at 23:57





Just a minor correction... OP's arrays start at 1

– Phil
Nov 20 '18 at 23:57













@Phil thanks, corrected.

– slider
Nov 20 '18 at 23:58





@Phil thanks, corrected.

– slider
Nov 20 '18 at 23:58




1




1





Upvote! First thing I though about when I read the question and then realized you already wrote it :)

– Akrion
Nov 21 '18 at 0:48





Upvote! First thing I though about when I read the question and then realized you already wrote it :)

– Akrion
Nov 21 '18 at 0:48













5














Using nested native Array#from()






const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)








share|improve this answer





















  • 2





    That's really really really really cool

    – zerkms
    Nov 21 '18 at 0:04






  • 1





    Array#from() 👈 spot the Java dev ;-)

    – Phil
    Nov 21 '18 at 0:05











  • I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

    – Phil
    Nov 21 '18 at 0:07











  • @Phil right...I assumed even 10's based on 20 and 1,000,000

    – charlietfl
    Nov 21 '18 at 0:09






  • 2





    It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

    – Mark Meyer
    Nov 21 '18 at 0:16


















5














Using nested native Array#from()






const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)








share|improve this answer





















  • 2





    That's really really really really cool

    – zerkms
    Nov 21 '18 at 0:04






  • 1





    Array#from() 👈 spot the Java dev ;-)

    – Phil
    Nov 21 '18 at 0:05











  • I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

    – Phil
    Nov 21 '18 at 0:07











  • @Phil right...I assumed even 10's based on 20 and 1,000,000

    – charlietfl
    Nov 21 '18 at 0:09






  • 2





    It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

    – Mark Meyer
    Nov 21 '18 at 0:16
















5












5








5







Using nested native Array#from()






const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)








share|improve this answer















Using nested native Array#from()






const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)








const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)





const 
limit = 100,
fr = Array.from;

const res = fr({ length:limit/10 }, (_,i) => fr({ length:10 }, (_,j) => i*10 + j+1 ));

console.log(res)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 1:20

























answered Nov 21 '18 at 0:03









charlietflcharlietfl

139k1388120




139k1388120








  • 2





    That's really really really really cool

    – zerkms
    Nov 21 '18 at 0:04






  • 1





    Array#from() 👈 spot the Java dev ;-)

    – Phil
    Nov 21 '18 at 0:05











  • I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

    – Phil
    Nov 21 '18 at 0:07











  • @Phil right...I assumed even 10's based on 20 and 1,000,000

    – charlietfl
    Nov 21 '18 at 0:09






  • 2





    It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

    – Mark Meyer
    Nov 21 '18 at 0:16
















  • 2





    That's really really really really cool

    – zerkms
    Nov 21 '18 at 0:04






  • 1





    Array#from() 👈 spot the Java dev ;-)

    – Phil
    Nov 21 '18 at 0:05











  • I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

    – Phil
    Nov 21 '18 at 0:07











  • @Phil right...I assumed even 10's based on 20 and 1,000,000

    – charlietfl
    Nov 21 '18 at 0:09






  • 2





    It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

    – Mark Meyer
    Nov 21 '18 at 0:16










2




2





That's really really really really cool

– zerkms
Nov 21 '18 at 0:04





That's really really really really cool

– zerkms
Nov 21 '18 at 0:04




1




1





Array#from() 👈 spot the Java dev ;-)

– Phil
Nov 21 '18 at 0:05





Array#from() 👈 spot the Java dev ;-)

– Phil
Nov 21 '18 at 0:05













I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

– Phil
Nov 21 '18 at 0:07





I know OP doesn't mention, but what if the limit isn't a multiple of 10, say 25? Slider's answer handles that case via chunking

– Phil
Nov 21 '18 at 0:07













@Phil right...I assumed even 10's based on 20 and 1,000,000

– charlietfl
Nov 21 '18 at 0:09





@Phil right...I assumed even 10's based on 20 and 1,000,000

– charlietfl
Nov 21 '18 at 0:09




2




2





It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

– Mark Meyer
Nov 21 '18 at 0:16







It's not as pretty, but you could have something like {length:Math.min(10, limit-(i*10))} in the inside loop to handle non-even groupings.

– Mark Meyer
Nov 21 '18 at 0:16













3














With vanilla javascript:






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





For anyone who's interested, I time clocked these answers and this is the fastest. Using 1 million entries, this clocks at .100 seconds. Lodash chunk clocks close behind at .110s. Array.from lags behind at .254s.






share|improve this answer


























  • Old school cool :)

    – slider
    Nov 21 '18 at 0:15






  • 1





    @Phil hah, fixed... and time-clocked

    – Conner
    Nov 21 '18 at 0:33
















3














With vanilla javascript:






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





For anyone who's interested, I time clocked these answers and this is the fastest. Using 1 million entries, this clocks at .100 seconds. Lodash chunk clocks close behind at .110s. Array.from lags behind at .254s.






share|improve this answer


























  • Old school cool :)

    – slider
    Nov 21 '18 at 0:15






  • 1





    @Phil hah, fixed... and time-clocked

    – Conner
    Nov 21 '18 at 0:33














3












3








3







With vanilla javascript:






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





For anyone who's interested, I time clocked these answers and this is the fastest. Using 1 million entries, this clocks at .100 seconds. Lodash chunk clocks close behind at .110s. Array.from lags behind at .254s.






share|improve this answer















With vanilla javascript:






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





For anyone who's interested, I time clocked these answers and this is the fastest. Using 1 million entries, this clocks at .100 seconds. Lodash chunk clocks close behind at .110s. Array.from lags behind at .254s.






x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)





x = 
for (i=1; i<=20; i=i+10) {
y =
for (j=0; j<10; j++) {
y.push(i + j)
}
x.push(y)
}
console.log(x)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 0:32

























answered Nov 21 '18 at 0:01









ConnerConner

23.3k84568




23.3k84568













  • Old school cool :)

    – slider
    Nov 21 '18 at 0:15






  • 1





    @Phil hah, fixed... and time-clocked

    – Conner
    Nov 21 '18 at 0:33



















  • Old school cool :)

    – slider
    Nov 21 '18 at 0:15






  • 1





    @Phil hah, fixed... and time-clocked

    – Conner
    Nov 21 '18 at 0:33

















Old school cool :)

– slider
Nov 21 '18 at 0:15





Old school cool :)

– slider
Nov 21 '18 at 0:15




1




1





@Phil hah, fixed... and time-clocked

– Conner
Nov 21 '18 at 0:33





@Phil hah, fixed... and time-clocked

– Conner
Nov 21 '18 at 0:33











3














Looking at the ES6 Array.from documentation you can see a range generator function:




const range = (start, stop, step) => Array.from({ length: (stop -
start) / step }, (_, i) => start + (i * step));



So with that in mind you could generate the range via:



range(1, 21, 1)  // account for the 0 with the 21 otherwise you get 1-19


Then all that is missing is the chunk function:






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





The chunkBy uses Array.reduce & the % operator to chunk the data.






share|improve this answer





















  • 1





    And you wrote a mini lodash :D

    – slider
    Nov 21 '18 at 0:50
















3














Looking at the ES6 Array.from documentation you can see a range generator function:




const range = (start, stop, step) => Array.from({ length: (stop -
start) / step }, (_, i) => start + (i * step));



So with that in mind you could generate the range via:



range(1, 21, 1)  // account for the 0 with the 21 otherwise you get 1-19


Then all that is missing is the chunk function:






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





The chunkBy uses Array.reduce & the % operator to chunk the data.






share|improve this answer





















  • 1





    And you wrote a mini lodash :D

    – slider
    Nov 21 '18 at 0:50














3












3








3







Looking at the ES6 Array.from documentation you can see a range generator function:




const range = (start, stop, step) => Array.from({ length: (stop -
start) / step }, (_, i) => start + (i * step));



So with that in mind you could generate the range via:



range(1, 21, 1)  // account for the 0 with the 21 otherwise you get 1-19


Then all that is missing is the chunk function:






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





The chunkBy uses Array.reduce & the % operator to chunk the data.






share|improve this answer















Looking at the ES6 Array.from documentation you can see a range generator function:




const range = (start, stop, step) => Array.from({ length: (stop -
start) / step }, (_, i) => start + (i * step));



So with that in mind you could generate the range via:



range(1, 21, 1)  // account for the 0 with the 21 otherwise you get 1-19


Then all that is missing is the chunk function:






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





The chunkBy uses Array.reduce & the % operator to chunk the data.






const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))





const range = (start, stop, step) => Array.from({ length: (stop - start) / step }, (_, i) => start + (i * step));
const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), )

console.log(chunkBy(range(1,21,1), 10))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 0:47

























answered Nov 21 '18 at 0:16









AkrionAkrion

9,42711224




9,42711224








  • 1





    And you wrote a mini lodash :D

    – slider
    Nov 21 '18 at 0:50














  • 1





    And you wrote a mini lodash :D

    – slider
    Nov 21 '18 at 0:50








1




1





And you wrote a mini lodash :D

– slider
Nov 21 '18 at 0:50





And you wrote a mini lodash :D

– slider
Nov 21 '18 at 0:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403334%2fuse-javascript-or-lodash-to-create-multi-dimensional-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith