There is a list of objects inside the main array products. What I want is to split this main array into sub arrays of 3 objects. I have tried something like :
render() {
console.log("All products", this.state.products);
let allProducts = this.state.products;
let size = 3;
let productDeckArr = [];
allProducts.map( (product, index) => {
productDeckArr.push(allProducts.slice(0, size));
});
console.log( "productDeckArr : ", productDeckArr);
I think the problem here is with map
which gives me the same sub array over and over again. I have tried this with for loops
and while
but seems to be unproductive.
What is the possible solution for this in order to break down the main array into sub-arrays each containing 3 objects and last sub-array may contain less than or equal to 3 ..?