Array chunking algorithm - Javascript

preview_player
Показать описание
Working with arrays in Javascript.
Рекомендации по теме
Комментарии
Автор

function chunk(arr, num){
const chunked = [];
for(let i = 0; i<arr.length; i+=num){
chuncked.push(arr.slice(i, i+num));
}
return chunked;
}

МихаилЛобанов-бз
Автор

Having some trouble wrapping my head around line 15, for some reason I really want to chunk.push(elem) and not last.push(elem). Could you explain a little why it's last.push(elem)? I think I'm getting confused because I look at line 11 where last is declared. Thanks!

youngbinpaek
Автор

Would this be O(n)? Since you have to go thru each element in the array right?

Hasansaid
welcome to shbcf.ru