How Do I Traverse A Javascript Object And Check For Nulls?
Solution 1:
You can really write:
(obj && obj.computers && obj.computers.favorite) || 'Unknown'
Solution 2:
You can do it with a helper function. If I understand your problem correctly, your objects may have arbitrary deep objects, and you want to access these arbitrary nested properties without cluttering your code. I built a Nested
function which lets you get()
arbitrary properties if they are set, or a default if they are not.
varNested = function() {};
// prop is a dot-separated path like "foo.bar.baz"Nested.prototype.get = function(prop, _default) {
var current = this;
$.each(prop.split("."), function(i, e) {
current = current[e] || undefined;
if (current == undefined)
returnfalse;
});
return current || _default;
}
You can then write code like this
var fav = obj.get("computers.favourite", "Linux");
// this emulatesvar fav = obj.computers.favourite || "Linux"; // throws error
As you can see, it's not so much more typing. Sure, it doesn't feel like regular Javascript... Here is the fiddle.
Solution 3:
I wrote this to help you deal with one of your questions: "I need to see if obj.computers.favorite has been set".
Object.prototype.isset = function (/* string */ full_path)
{
var props = full_path.split('.');
varself = this; /* note that self is usually the window object */for (var ii = 0; ii < props.length; ++ii)
{
var prop = props[ii];
var hasMoreComing = ii < props.length - 1 ? true : false;
if (self[prop] !== null && typeof self[prop] === 'object' && hasMoreComing)
{
self = self[prop];
continue; // Move up one level.
}
elseif (hasMoreComing)
returnfalse; // ..because user queries a subproperty of a value typereturnself.hasOwnProperty(prop);
}
};
Test-code:
var test = {};
test.kuk = {};
console.log( test.isset('kuk') ); // Prints true.
test.kuk.fitta = {};
console.log( test.isset('kuk.fitta') ); // Prints true.
test.kuk.fitta = null;
console.log( test.isset('kuk.fitta') ); // Prints true.
test.kuk.fitta = undefined;
console.log( test.isset('kuk.fitta') ); // Prints truedelete test.kuk.fitta;
console.log( test.isset('kuk.fitta') ); // Prints false
test.kuk.fitta = 123;
console.log( test.isset('kuk.fitta.doesnt.exist') ); // Prints false
Solution 4:
Unfortunately, there's not a super easy way to get around this, but you don't need to check for both null and undefined. Because null and undefined are both falsey, you can just do:
if (obj && obj.computers) {
var fav = obj.computers.favorite || 'unknown';
}
It doesn't actually get around what your complaint is, but it's less painful than what you'd think.
Solution 5:
following function will take string as parameter and return object if exist
functiongetValueOrDefault(str , obj, deflt){
var a = str.split("."); o = obj;
for(var i =0; i < a.length; i++){
o = obj[a[i]];
if(!o){
return deflt;
}
}
return o;
}
var obj = {};
obj.computers = {};
obj.computers.favorite = "Commodore 64";
obj.computers.popular = "Apple";
getValueOrDefault('computers.favorite', obj, 'Unknown');
Note: You must not use var while assigning properties to object eg. var obj.computers.favorite
is SYNTAX error.
Post a Comment for "How Do I Traverse A Javascript Object And Check For Nulls?"