Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, December 24, 2013

Size of dictionary in jquery

One would think that they can apply size of length function directly and get it, but its little more than that. You can not apply the length property or size function, both only works with list.

Dict is an Object and it doesn't have size method, and if you do length it will give 'undefined'.

Solution - Get the array of keys and perform length.

e.g.
> dictObj = Object {PointAArray[23]PointBArray[23]}
Object.keys(dictObj).length
> 2

Note - It won't works > IE8

Other robust way is as described in this SO answer, add size function on Object and use it across the app and it will work cross browser.

Short answer is there is no built-in way.

Sunday, November 4, 2012

Null Check in different language


Python - Null Check

15 or "default"       # returns 15
0 or "default"         # returns "default"
None or "default"    # returns "default"
False or "default"    # returns "default"

Django Template - None Check


{{obj.item_value|default_if_none:"smile"}}

C# - Null Check

var data = val ?? "default value";

Java - Null check

Foo f = new Foo();
DummyObj obj = new Obj().getSelection();
String str = obj != null ? f.format(obj) : "";

Javscript - Null/Undefined check

if (! param) param = "abc";
//other way to check this
if (param == null) is same as if(!param)


jQuery - Null/existence check

if ( $('#myDivObj').length ) {}

Feel free to add comments and other languages Null check mechanism (inline or explicit)