I'm having an issue creating an object in the correct format when adding to it from a loop.
e.g. I loop through some lists
<ul class="pdmenu">
<ul class="pdmenu">
<li class="menu-top" id="vmen-1"><a href="#">1</a></li>
<li class="menu-item"><a href="#">aaa</a></li>
</ul>
<ul class="pdmenu">
<li class="menu-top" id="vmen-2"><a href="#">2</a></li>
<li class="menu-item"><a href="#">aaa</a></li>
<li class="menu-item"><a href="#">bbb</a></li>
</ul>
<ul class="pdmenu">
<li class="menu-top" id="vmen-3"><a href="#">3</a></li>
<li class="menu-item"><a href="#">aaa</a></li>
<li class="menu-item"><a href="#">bbb</a></li>
</ul>
I use jQuery to loop through the top list item, .menu-top, and push the id and visibility to an object.
jsonmenu = $(); // Set empty object.
$('.menu-top').each(function(index) {
jsonmenu.push({
menu: $(this).attr('id'),
visible: "" + $(this).next().is(':visible') + ""
});
});
This creates the object with member for each item like so
{
"0": {
"menu":"vmen-1",
"visible":"false"
},
"length":4,
"1": {
"menu":"vmen-2",
"visible":"false"
},
"2":{
"menu":"vmen-3",
"visible":"false"
},
"3": {
"menu":"vmen-4",
"visible":"true"
}
}
All I need is a simple format like the following;
{
"menu":"vmen-1",
"visible":"false"
},
{
"menu":"vmen-2",
"visible":"false"
},
{
"menu":"vmen-3",
"visible":"false"
},
{
"menu":"vmen-4",
"visible":"true"
}
How can I change this to get the object in this simple format?
object- nothing to do with json. I've amended your terminology slightly to save confusion.$()creates a jQuery instance and is absolutely not suited to be serialized. Why did you use it?