var menu = [];
$('.mainmenu').click(function() {
$('.mainmenu').each(function(i,v) {
var sub = {};
var indexmenu = $(this).attr('id');
sub[indexmenu] = $(this).attr('data-currstate');
menu.push(sub);
})
$.each(menu,function(i,v) {
console.log(i)
console.log(v)
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a target="_top" href="#" class='mainmenu' id='maintenance' data-currstate='hide'>
<i class="fa fa-cogs"></i> Maintenance
</a>
I want to create an array of object from a menu wherein I want the ID of each menu to be the index and the value will be state.
- I want the id of each menu to be the index and the state to be the value
- What i get is the id and value becomes the value
Note:
In my actual code the creation of the menu array happens on load of page in demo I created it on click. Also the reason why I created on load is that I store the value in local storage. I made the demo as simple as I can
maintenanceas index instead of0andhideas value instead of{"maintenance":"value"}{ "0": "hide" }?maintenance