Search code examples
javascriptuserscripts

How to call .get() method on this JavaScript array?


First, look at this screenshot:

Console

I would like to call .get("p" + number) on this, but however I try to do this I get undefined or this:

Global.refPatients["_object"].get(p151833309)

Uncaught TypeError: Global.refPatients._object.get is not a function
    at <anonymous>:2:31
    at Object.InjectedScript._evaluateOn (<anonymous>:895:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)
    at Object.InjectedScript.evaluate (<anonymous>:694:21)

Solution

  • As pointed out in the comments, objects don't have a get method. If you want to access the value of a particular property, you can just reference the property with either the bracket or the dot notation (see Property Accessors on MDN):

    //dot notation:
     Global.refPatients["_object"].p151833309;
    
    //bracket notation - needed if you want to get 15183309 from a variable:
    Global.refPatients["_object"]["p"+151833309];