0

I need to create multi dimensional array in JavaScript

var collection = new Array();
$(document).on('change', 'input[type="checkbox"]', function(e) {
    if (this.checked) {
        var type = $(this).data('type');
        var id = $(this).data('id');

        collection[type].push(id);
    }
});

Uncaught TypeError: Cannot read property 'push' of undefined

I need to convert these values into JSON format later on.

Any idea how to fix this?

1
  • 1
    Why not just do var collection = {} and create an object instead, seems more appropriate for this. Commented May 23, 2014 at 15:40

3 Answers 3

1

You could create an an object which is more appropriate in this case:

var collection = {};
$(document).on('change', 'input[type="checkbox"]', function(e) {
    if (this.checked) {
        var type = $(this).data('type');
        var id = $(this).data('id');

        if (!collection[type]) {
           collection[type] = [];
        }
        collection[type].push(id);
    }
});   
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works perfectly, will accept your answer once allowed
1

You have to define array:

//var collection = new Array(); //for array index must be ineger
var collection = {}; //for not numeric indexes
$(document).on('change', 'input[type="checkbox"]', function(e) {
    if (this.checked) {
        var type = $(this).data('type');
        var id = $(this).data('id');
        if('array' !== typeof collection[type]) collection[type] = new Array();
        collection[type].push(id);
    }
});

1 Comment

i need type to be string? is there any workaround for this? following your answer, only latest value is recorded, the previous one is not.
1

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.