Steve Cross
Steve Cross

Reputation: 15

Javascript: Adding an element to an associative array

I am trying to build a menu for use with jQuery contextMenu. It will be dynamic. I'm trying to add one element to it but it's reporting the error 'undefined is not a function'.

My menu works without the code that is trying to push the new element.

//Build the menu
var menudata = {
    "Download Call": {name: "Download_Call"},
    "sep1": "---------",
    "View Comments": {name: "View_Comments"}
};

menudata.push (
    {
        "test": {name: "test"}
    }
);

//Generate the context menu
$.contextMenu({
    selector: '.context-menu-one',
    trigger: 'left',
    callback: function(key, options) {
        var m = "clicked: " + key + " on " + $(this).attr('class');
        alert(m);
    },
    items: menudata
});

I'm assuming it's a datatype issue but any help would be greatly appreciated.

Upvotes: 0

Views: 771

Answers (1)

antyrat
antyrat

Reputation: 27765

Object's doesn't have push method. Only Array's have. You can try this instead:

menudata[ "test" ] = {name: "test"};

Upvotes: 4

Related Questions