I have an array like this
animals = [Dog,Cat,Mouse,Tiger,Lion,Bird,Horse]
I want to break it and display this array in the below format
0: [Dog,Cat,Mouse]
1: [Tiger,Lion,Bird]
2: [Horse]
For that I have written a function like this
splitPairs(arr) {
var pairs = [];
for (var i=0 ; i<arr.length ; i+=3) {
if (arr[i+1] !== undefined && arr[i+2] !== undefined) {
pairs.push ([arr[i], arr[i+1], arr[i+2]]);
} else if(arr[i+1] !== undefined) {
pairs.push ([arr[i], arr[i+1]]);
} else {
pairs.push ([arr[i]]);
}
}
this.animals = pairs;
When I'm using splitPairs(animals)
function everything is working fine.
BUT THE PROBLEM IS
I dont know that breakup count (which is in this case is 3)
Suppose that animal
array will break after 5
or say 10
then how to use that function with a loop so that it will break automatically.
Say splitPairs(animals, 5)
I pass 5
in the second parameter and it will break automatically after 5th index.
I hope I make you understand my problem.
Any kind of help would be highly appreciated.