I'm looking to accomplish something similar to the _.chunk() function in lodash. The chunk function breaks an array into equal parts, based on the quantity of objects in the original array.
However, I want to break a Javascript array into smaller arrays based on the quantity of the object's subitems. So the new arrays can have a varying number of elements, but the quantity of subitems in each new array must total 3(except for the last array if there is a remainder).
For example, I want to use Javascript to convert this array:
[ {name:'John', numberOfPets: 5,
pets:['petOne', 'petTwo', 'petThree', 'petFour', 'petFive']},
{ name:'Andrew', numberOfPets: 5,
pets: ['petSix', 'petSeven', 'petEight', 'petNine', 'petTen']}]
Into these arrays:
[{name: 'John', numberOfPets: 5, pets: ['petOne', 'petTwo', 'petThree']}],
[{name: 'John', numberOfPets: 5, pets: ['petFour', 'petFive']},
{name: 'Andrew', numberOfPets: 5, pets: ['petSix']}],
[{name: 'Andrew', numberOfPets: 5, pets: ['petSeven', 'petEight',
'petNine']}],
[{name: 'Andrew', numberOfPets: 5, pets: ['petTen']}]
You will notice the new arrays each have 3 pets, except for the last array which just has 1 remainder.
Any help would be greatly appreciated.
petsproperty of each object is invalid as posted above.