“JavaScript Array Split Chunk” Code-Antworten

JavaScript Array Split Chunk

const chunk = (arr, size) => arr.reduce((acc, e, i) => (i % size ? acc[acc.length - 1].push(e) : acc.push([e]), acc), []);

// Examples
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3);     // [[1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4);     // [[1, 2, 3, 4], [5, 6, 7, 8]]
Batman

JavaScript teilte Array in Sko auf

function splitArrayIntoChunksOfLen(arr, len) {
  var chunks = [], i = 0, n = arr.length;
  while (i < n) {
    chunks.push(arr.slice(i, i += len));
  }
  return chunks;
}
var alphabet=['a','b','c','d','e','f'];
var alphabetPairs=splitArrayIntoChunksOfLen(alphabet,2); //split into chunks of two
Grepper

Array Chunk JavaScript

let input = [1,2,3,4,5,6,7,8,9];
let chunked = []
let size = 2;

for (let i = 0;  i < input.length; i += size) {
  chunked.push(input.slice(i, i + size))
}
console.log(chunked)
Restu Wahyu Saputra

Array Chunk JavaScript

let input = [1,2,3,4,5,6,7,8];
let chunked = []
let size = 2;

Array.from({length: Math.ceil(input.length / size)}, (val, i) => {
  chunked.push(input.slice(i * size, i * size + size))
})

console.log(chunked);
Restu Wahyu Saputra

Ähnliche Antworten wie “JavaScript Array Split Chunk”

Fragen ähnlich wie “JavaScript Array Split Chunk”

Weitere verwandte Antworten zu “JavaScript Array Split Chunk” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen