The array.slice() method can extract a slice from the beginning, middle, or end of an array for whatever purposes you require, without changing the original array.
const chunkSize = 10;
for (let i = 0; i < array.length; i += chunkSize) {
const chunk = array.slice(i, i + chunkSize);
// do whatever
}
The last chunk may be smaller than chunkSize. For example when given an array of 12 elements the first chunk will have 10 elements, the second chunk only has 2.
Note that a chunkSize of 0 will cause an infinite loop.
Videos
The array.slice() method can extract a slice from the beginning, middle, or end of an array for whatever purposes you require, without changing the original array.
const chunkSize = 10;
for (let i = 0; i < array.length; i += chunkSize) {
const chunk = array.slice(i, i + chunkSize);
// do whatever
}
The last chunk may be smaller than chunkSize. For example when given an array of 12 elements the first chunk will have 10 elements, the second chunk only has 2.
Note that a chunkSize of 0 will cause an infinite loop.
Here's a ES6 version using reduce
const perChunk = 2 // items per chunk
const inputArray = ['a','b','c','d','e']
const result = inputArray.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index/perChunk)
if(!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [] // start a new chunk
}
resultArray[chunkIndex].push(item)
return resultArray
}, [])
console.log(result); // result: [['a','b'], ['c','d'], ['e']]
And you're ready to chain further map/reduce transformations. Your input array is left intact
If you prefer a shorter but less readable version, you can sprinkle some concat into the mix for the same end result:
inputArray.reduce((all,one,i) => {
const ch = Math.floor(i/perChunk);
all[ch] = [].concat((all[ch]||[]),one);
return all
}, [])
You can use remainder operator to put consecutive items into different chunks:
const ch = (i % perChunk);
I have an array like this
[
{id:1, category:'category 1'},
{id:2, name:'category 2'},
{id:3, name:'category 1'},
{id:4, name:'category 3'},
]I wanna convert that into an array like this
[
[
{id:1, category:'category 1'},
{id:3, name:'category 1'},
],
[
{id:2, name:'category 2'},
],
[
{id:4, name:'category 3'},
]
]
I don't know in advance the number of categories, any ideas how to achieve this?
I would seriously reconsider that data structure if you can help it, but here is a solution:
const obj = {}
for (let i = 0; i < arr.length; i++) {
const category = arr[i].category || arr[i].name
if (!obj.hasOwnProperty(category)) {
obj[category] = []
}
obj[category].push(arr[i])
}
const result = Object.values(obj)
Using generic groupBy function (taken from this lib):
const groupBy = (groupingFn, arr) => {
const groups = {};
arr.forEach((item) => {
const key = groupingFn(item);
if (groups[key] !== undefined) {
groups[key].push(item);
} else {
groups[key] = [item];
}
});
return groups;
};
const arr = [
{ id: 1, category: "category 1" },
{ id: 2, name: "category 2" },
{ id: 3, name: "category 1" },
{ id: 4, name: "category 3" }
];
const grouped = Object.values(
groupBy((item) => item.name || item.category, arr)
);
console.log(grouped);