Ich habe ein assoziatives Array wie folgt. Ich muss es in zwei oder mehr Teile aufteilen, wenn Unterkategorien mehr als 5 sind. Ist es möglich? Ich brauche Javascript-Code, um dieses Array dynamisch so zu teilen, dass eine Kategorie nur 5 Unterkategorien haben sollte und wenn es mehr als 5 Unterkategorien in einer Kategorie gibt, sollte es eine weitere Kategorie mit demselben Namen mit den übrig gebliebenen Unterkategorien erstellen.Split assoziative Array
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"
}
]
}]
Erwartetes Ergebnis sollte
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"
}
]
}
]
wie willst du es teilen? Was ist das erwartete Ergebnis? – kukkuz
was soll mit den gespaltenen Artikeln passieren? –
Erwartete Ergebnis sollte –