The following array is given:
[
{
"id": "34285952",
"labs": [
{
"id": "13399-17",
"location": "Gambia",
"edge": ["5062-4058-8562-2942-2107-2064-58"]
}
]
},
{
"id": "85130775",
"labs": [
{
"id": "52504-72",
"location": "Nepal",
"edge": [
"5232-9427-8339-7218-3936-9389-52",
"6375-9293-7064-5043-6869-4773-65",
"8547-4739-6334-3896-7208-8243-67"
]
}
]
},
{
"id": "67817268",
"labs": [
{
"id": "17891-68",
"location": "U.S.",
"edge": [
"6383-7536-7257-4713-9494-9910-93",
"6743-8803-1251-1173-5133-2107-19"
]
}
]
},
.... possibly more objects but removed to keep example short
]
I want to write a function which is taking a number as parameter.
Based on this number, for example 10
, I want to merge ten objects into an Arrays of Array.
If I have 22 objects in my array, then I want to have three arrays in my array.
Input:
[ { }, { }, { }, { }, { }, { }, { }, { }, ]
Output (group by two):
[ [ { }, { } ], [ { }, { } ], [ { }, { } ], [ { }, { } ] ]
My approach would be something like but I do not figure out how to check how many objects I have and then merge them into a new array.
// array = example above
// countOption = 10
const splitArrayByCount = (array, countOption) => {
return array.map(object => {
if (array.length > countOption) {
return [object]
}
})
};
The idea is to use this for pagination.