The reason that this is not working is that you are trying to push it into a multidimentional array, but the array you are attempting to push to doesnt exist.
var collection = [];
$(document).on('change', 'input[type="checkbox"]', function(e) {
if (this.checked) {
var type = $(this).data('type');
var id = $(this).data('id');
if (typeof collection[type] === 'undefined') {
collection[type] = [];
}
collection[type].push(id);
}
});
That should work. Just so you know, it is much better to use [] instead of new Array(). As calling new Array() can have very strange behavior and can be confusing for beginners, for the most part stick with `[].
Also i have added if (typeof collection[type] === 'undefined') {
What this will do is check if the index inside the collection array is undefined. If it is, that means there is no array to push the id to. So we create the array of the type inside collection with the line below it:
collection[type].push(id);
Hope that makes sense to you, I remember multimentional arrays being confusing too!!
var collection = {}and create an object instead, seems more appropriate for this.