I have an associative array as below. I need to split it in 2 or more parts whenever subcategories are more than 5. Is it possible ? I need javascript code to split this array dynamically so that one category should have only 5 subcategories and if there are more than 5 subcategories in a category, it should create one more category with same name with the leftover subcategories.
var obj = [
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf5a3c3ed2bd632c06225",
"subCategoryName": "Grey goose"
},
{
"subCategoryId": "57fdf5fcc3ed2bd632c06227",
"subCategoryName": "Absolut Elyx"
},
{
"subCategoryId": "57fdf5c2c3ed2bd632c06226",
"subCategoryName": "Belvedere"
},
{
"subCategoryId": "57fdf627c3ed2bd632c06229",
"subCategoryName": "Absolut"
},
{
"subCategoryId": "57fdf61fc3ed2bd632c06228",
"subCategoryName": "Finlandia"
},
{
"subCategoryId": "57fdf64cc3ed2bd632c0622a",
"subCategoryName": "Absolut flavoured"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf163c3ed2bd632c06217",
"subCategoryName": "Blue lable"
},
{
"subCategoryId": "57fdf236c3ed2bd632c06218",
"subCategoryName": "Royal sulte"
},
{
"subCategoryId": "57fdf24ac3ed2bd632c06219",
"subCategoryName": "Chivas 18yr"
},
{
"subCategoryId": "57fdf2bfc3ed2bd632c0621a",
"subCategoryName": "J/W Gold lable"
},
{
"subCategoryId": "57fdf2f9c3ed2bd632c0621d",
"subCategoryName": "Chivas extra"
},
{
"subCategoryId": "57fdf2d7c3ed2bd632c0621b",
"subCategoryName": "Chivas 12yr"
},
{
"subCategoryId": "57fdf2edc3ed2bd632c0621c",
"subCategoryName": "J/W Black lable"
},
{
"subCategoryId": "57fdf32ac3ed2bd632c0621e",
"subCategoryName": "J/W Red lable"
}
]
}]
Expected result should be
var expectedResult = [
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf5a3c3ed2bd632c06225",
"subCategoryName": "Grey goose"
},
{
"subCategoryId": "57fdf5fcc3ed2bd632c06227",
"subCategoryName": "Absolut Elyx"
},
{
"subCategoryId": "57fdf5c2c3ed2bd632c06226",
"subCategoryName": "Belvedere"
},
{
"subCategoryId": "57fdf627c3ed2bd632c06229",
"subCategoryName": "Absolut"
},
{
"subCategoryId": "57fdf61fc3ed2bd632c06228",
"subCategoryName": "Finlandia"
},
]
},
{
"categoryId": "57f22e84c3ed2bd632c061bf",
"categoryName": "VODKA"
},
"subCategory": [
{
"subCategoryId": "57fdf64cc3ed2bd632c0622a",
"subCategoryName": "Absolut flavoured"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf163c3ed2bd632c06217",
"subCategoryName": "Blue lable"
},
{
"subCategoryId": "57fdf236c3ed2bd632c06218",
"subCategoryName": "Royal sulte"
},
{
"subCategoryId": "57fdf24ac3ed2bd632c06219",
"subCategoryName": "Chivas 18yr"
},
{
"subCategoryId": "57fdf2bfc3ed2bd632c0621a",
"subCategoryName": "J/W Gold lable"
},
{
"subCategoryId": "57fdf2f9c3ed2bd632c0621d",
"subCategoryName": "Chivas extra"
}
]
},
{
"categoryId": "57f22f18c3ed2bd632c061c4",
"categoryName": "SCOTCH WHISKY"
},
"priority": 2,
"subCategory": [
{
"subCategoryId": "57fdf2d7c3ed2bd632c0621b",
"subCategoryName": "Chivas 12yr"
},
{
"subCategoryId": "57fdf2edc3ed2bd632c0621c",
"subCategoryName": "J/W Black lable"
},
{
"subCategoryId": "57fdf32ac3ed2bd632c0621e",
"subCategoryName": "J/W Red lable"
}
]
}
]