Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of .get in javascript?

Tags:

d = {'hello':'abc'}
d.get('hello','default_val');

Above is python. How to do this in javascript? I want to be able to set a default value if no key found.

like image 578
TIMEX Avatar asked Jul 28 '11 07:07

TIMEX


People also ask

What does'$ mean in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).

What is the this operator in JavaScript?

What is this? In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

Why to use this in JavaScript?

In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.

What does get means in JavaScript?

The get syntax binds an object property to a function that will be called when that property is looked up.

What is type conversion in JavaScript?

In programming, type conversion is the process of converting data of one type to another. For example: converting String data to Number. There are two types of type conversion in JavaScript.

What is $ (document).ready () equivalent in JavaScript?

What is $ (document).ready () equivalent in JavaScript? Javascript Web Development Front End Technology In jQuery, if you want an event to work on your page, you should call it inside the $ (document).ready () function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

How to convert strings to numbers in JavaScript?

In JavaScript, explicit type conversions are done using built-in methods. Here are some common methods of explicit conversions. 1. Convert to Number Explicitly To convert numeric strings and boolean values to numbers, you can use Number (). For example, In JavaScript, empty strings and null values return 0.

How to convert a date to a number in JavaScript?

The global method Number () can be used to convert dates to numbers. The date method getTime () does the same. When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type. JavaScript automatically calls the variable's toString () function when you try to "output" an object or a variable:


1 Answers

You have (at least) four options:

  1. In many cases, you can use the curiously-powerful || operator:

    x = obj.key || "default";
    

    That means: Set x to obj.key unless obj.key is falsy, in which case use "default" instead. The falsy values are undefined, null, 0, NaN, "", and of course, false. So you wouldn't want to use it if obj.key may validly be 0 or any other of those values.

  2. For situations where || isn't applicable, there's the in operator:

    x = "key" in obj ? obj.key : "default";
    

    in tells us whether an object has a property with the given key. Note the key is a string (property names are strings or Symbols; if you were using a Symbol, you'd know). So if obj.key may be validly 0, you'd want to use this rather than #1 above.

  3. in will find a key if it's in the object or the object's prototype chain (e.g., all of the places you'd get it from if you retrieve the property). If you just want to check the object itself and not its prototype chain, you can use hasOwnProperty:

    x = obj.hasOwnProperty("key") ? obj.key : "default";
    
  4. Specifically check for undefined:

    x = typeof obj.key !== "undefined" ? obj.key : "default";
    

    That will use the default if obj doesn't have that property or if it has the property, but the property's value is undefined.

like image 59
T.J. Crowder Avatar answered Sep 20 '22 07:09

T.J. Crowder