Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created February 24, 2015 09:30
Show Gist options
  • Save sharpred/d3c790c7f92841ed70a3 to your computer and use it in GitHub Desktop.
Save sharpred/d3c790c7f92841ed70a3 to your computer and use it in GitHub Desktop.
deep nested property
function propDeep(target, path, rtn) {
"use strict";
try {
target = target || {};
var result = eval("target." + path);
} catch(e) {
return false;
}
if (result) {
return rtn ? result : true;
}
}
function hasProperty(target, path) {
var test = propDeep(target, path, false);
return test;
}
function getProperty(target, path) {
var test = propDeep(target, path, true);
return test;
}
exports.hasProperty = hasProperty;
exports.getProperty = getProperty;
var deepHas = require("./deepHas");
var _ = require("underscore");
_.mixin(deepHas);
var homeAPIResponse = {
onlineCheckInConfig : {
flightCheckIn : {
sniffUrl : "http://sniffthat"
}
}
};
//will return http://sniffthat
console.log(_.getProperty(homeAPIResponse, "onlineCheckInConfig.flightCheckIn.sniffUrl"));
//will return true
console.log(_.hasProperty(homeAPIResponse, "onlineCheckInConfig.flightCheckIn.sniffUrl"));
//will both return undefined
console.log(_.getProperty(homeAPIResponse, "onlineCheckInConfig.flightCheckIn.sniffURl"));
console.log(_.hasProperty(homeAPIResponse, "onlineCheckInConfig.flightCheckIn.sniffURl"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment