I have a json object that has properties into a type and product. I am looking for a way to loop through the elements and organize them into a tiered menu using YUI menu so the first level would group them by type and then the second menu would group them by product.
The json looks something like:
[{ "productId":1, "typeId": 1, "productName": "test", "typeName": "old", "itemId": 1, "itemName": "item1"},
{ "productId":2, "typeId": 2, "productName": "test2", "typeName": "new", "itemId": 2, "itemName": "item2"},
{ "productId":2, "typeId": 1, "productName": "test2", "typeName": "old", "itemId": 3, "itemName": "item3"}]
I would like to be able to loop through the items and add them to their correct submenu through looping, but the way that YUI menu is structured, doesn't seem to be an easy way to do this. The resulting menu structure would be something like:
- old
- test
- item1
- test2
- item3
- test
- new
- test2
- item2
- test2
Question clarification: Looping through and creating individual items is easy enough:
for ( var i in obj )
{
menu.addItem(obj[i].itemName);
}
What I need to do is to loop through creating submenus when they don't exists and appending the items to the submenus. All told there will be potentially up to like 200 items that will need to be sorted into these submenus so each branch will have several items. I'm looking for an easy way to do the check/create/append workflow.