How to get value from Object, with default value

JavascriptObject

Javascript Problem Overview


I constantly find myself passing config values to functions accessing them like this:

var arg1 = 'test1';
if(isUndefined(config.args.arg1)){
  arg1 = config.args.arg1;
} 

var arg2 = 'param2';
if(isUndefined(config.args.arg2)){
  arg2 = config.args.arg2;
} 

var arg3 = '123';
if(isUndefined(config.args.arg3)){
  arg3 = config.args.arg3;
} 

where I later use them like this:

var url = '<some-url>?id='+arg1+'&='+arg2 +'=' + arg3;

Does jQuery/ExtJS or any other framework provide a solution to access variables like this in a simple way, and give variables a default value?

Something like:

getValueOfObject(config,'args.arg3','<default>');

Or is there maybe a standard solution for this.

NOTE:

I was also thinking about the common pattern where you have defaults

var defaults = {
   args: {
      args1: ....
   }
   ...
}

and doing an object merge.

And then encoding the object to a param String. But as you can see the object values also sometimes contain parameter names.

Javascript Solutions


Solution 1 - Javascript

Generally, one can use the or operator to assign a default when some variable evaluates to falsy:

var foo = couldBeUndefined || "some default";

so:

var arg1 = config.args.arg1 || "test";
var arg2 = config.args.arg2 || "param2";

assuming that config.args is always defined, as your example code implies.

Solution 2 - Javascript

Looks like finally lodash has the _.get() function for this!

Solution 3 - Javascript

With ECMAScript 2020 it may be safer to use Nullish coalescing operator than or operator.

let foo = obj.maybeUndefined ?? "default value";

JS treats 0, -0, null, false, NaN, undefined, or the empty string ("") as falsy. So, using or operator for setting a default value in the following example may cause unexpected results

let obj = { maybeUndefined: 0 };
let foo = obj.maybeUndefined || 1; // foo == 1

Here, foo becomes equal to 1, when maybeUndefined is actually defined. If foo should be assigned a default value only when maybeUndefined is actually undefined or null, then we can use Nullish coalescing operator.

let obj = { maybeUndefined: 0 };
let foo = obj.maybeUndefined ?? 1; // foo == 0

let obj = {};
let foo = obj.maybeUndefined ?? 1; // foo == 1

Solution 4 - Javascript

With ES2018, you can now write options = { ...defaults, ...options }:

Spread syntax - JavaScript | MDN

> Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax than Object.assign(). > > const obj1 = { foo: 'bar', x: 42 }; > const obj2 = { foo: 'baz', y: 13 }; >
> const clonedObj = { ...obj1 }; > // Object { foo: "bar", x: 42 } >
> const mergedObj = { ...obj1, ...obj2 }; > // Object { foo: "baz", x: 42, y: 13 }

Solution 5 - Javascript

try var options = extend(defaults, userOptions);

This way you get all the userOptions and fall back to defaults when they don't pass any options.

Note use any extend implementation you want.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremy S.View Question on Stackoverflow
Solution 1 - Javascriptkarim79View Answer on Stackoverflow
Solution 2 - JavascriptJeremy S.View Answer on Stackoverflow
Solution 3 - JavascriptTimur OsadchiyView Answer on Stackoverflow
Solution 4 - JavascriptClémentView Answer on Stackoverflow
Solution 5 - JavascriptRaynosView Answer on Stackoverflow