jquery - Sort javascript Object in Array -
i want make javascript dynamic menu, , i'm having few troubles that, json data , javascript want setup menu , submenus...
the problem
listmenu[ { id:10, name: 'financial', submenu: 0, icon: 'fa-bar-chart' }, { id:11, name: 'bill', submenu: 10, icon: 'fa-bar-chart' }, { id:12, name: 'pay', submenu: 10, icon: 'fa-bar-chart' }, { id:13, name: 'contact', submenu: 2, icon: 'fa-bar-chart' } ]
what need relation submenu
id
this:
financial (submenu 0)
bill (submenu 10)
pay (submenu 10)
contact
if want sort array using sort of custom logic, use array.prototype.sort
in case, this:
var listmenu = [ {id:10, name: 'financial', submenu: 0, icon: 'fa-bar-chart'}, {id:11, name: 'bill', submenu: 10, icon: 'fa-bar-chart'}, {id:12, name: 'pay', submenu: 10, icon: 'fa-bar-chart'}, {id:13, name: 'contact', submenu: 2, icon: 'fa-bar-chart'} ] var sortedlist = listmenu.sort( function( a, b ){ if( a.submenu > b.submenu ){ return 1 } else if ( b.submenu > a.submenu ){ return -1 } else { return 0 } })
Comments
Post a Comment