I have a list like this:
<li class='category parent-category' data-categoryId='1' data-parentCategoryId='' data-parentOrder='1'>
<a href='/category/edit/1'>Business Basics</a>
</li>
<li class='category parent-category' data-categoryId='10' data-parentCategoryId='1' data-parentOrder='2'>
<a href='/category/edit/10'>General/Legal</a>
</li>
<li class='category parent-category' data-categoryId='15' data-parentCategoryId='2' data-parentOrder='3'>
<a href='/category/edit/15'>Home, Help & Links</a>
</li>
I want to loop over every element with the class 'category', get the data attributes, and insert those into an array.
var categoryData = Object.create(null);
var myArray = [];
$('.category').each(function() {
categoryData.categoryId = category.attr('data-categoryId');
categoryData.parentCategoryId = category.attr('data-parentCategoryId');
categoryData.childOrder = category.attr('data-child-order');
categoryData.parentOrder = category.attr('data-parent-order');
myArray.push(categoryData);
});
But currently it only grabs the data from the first element and then inserts that into the array multiple times.
[
0 => [ categoryId => 1, parentCategoryId => , childOrder => , parentOrder => 1 ],
1 => [ categoryId => 1, parentCategoryId => , childOrder => , parentOrder => 1 ],
2 => [ categoryId => 1, parentCategoryId => , childOrder => , parentOrder => 1 ]
]
What I want is:
[
0 => [ categoryId => 1, parentCategoryId => , childOrder => , parentOrder => 1 ],
1 => [ categoryId => 10, parentCategoryId => 1, childOrder => , parentOrder => 2 ],
2 => [ categoryId => 15, parentCategoryId => 2, childOrder => , parentOrder => 3 ]
]