0

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.

4
  • Which line are you getting the error on? Commented Jul 29, 2014 at 15:12
  • You have an object - JS doesnt have associative arrays. Commented Jul 29, 2014 at 15:13
  • Note that associative arrays (yes, in js objects implements the concept of associative arrays but that's just a naming issue nothing fundamental) in any language is supposed to be unordered. By happy accident IE, Firefox, Safari and Chrome all retain creation order. However, this is never guaranteed in any specification. And indeed the ECMA specification specifically states that order is not retained. Any code that expects an object's order to be retained like the one you have should be considered buggy. Commented Jul 29, 2014 at 15:21
  • One should especially note that the fastest/most efficient implementation of associative arrays - a hash table (or hash tree) has pseudo-random order depending on the result of the hashing function over the key. Commented Jul 29, 2014 at 15:23

1 Answer 1

4

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

menudata[ "test" ] = {name: "test"};
Sign up to request clarification or add additional context in comments.

2 Comments

or dot syntax: menudata.test = {name: "test"};
@Jason yes, but as I can see from OP code he have some Object properties with space char in name so better is to use brackets in this case.

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.