Я пытаюсь разбить массив сына на куски из 3 внутренних массивов.
ex:
[{
title: "asd",
description: "asd"
}, {
title: "asz",
description: "sd"
}, {
title: "ws",
description: "sd"
}, {
title: "re",
description: "sd"
}, {
title: "32",
description: "xxs"
}, {
title: "xxc",
description: "11"
}]
Я хочу преобразовать массив верхнего сына в куски, как показано ниже
[
[{
title: "asd",
description: "asd"
}, {
title: "asz",
description: "sd"
}, {
title: "ws",
description: "sd"
}],
[{
title: "re",
description: "sd"
}, {
title: "32",
description: "xxs"
}, {
title: "xxc",
description: "11"
}]
]
Я пробовал подобный пример, но он работает для этого массива ['a','b','c','d','e']
отлично.ниже приведен код, который я пробовал,
perChunk = 2 // items per chunk
inputArray = ['a', 'b', 'c', 'd', 'e']
inputArray.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / perChunk)
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [] // start a new chunk
}
resultArray[chunkIndex].push(item)
console.log(resultArray)
return resultArray
}, [])
Но для моего ввода это не работает.
Пожалуйста, подскажите, как мне этого добиться