Created
February 24, 2015 09:30
-
-
Save sharpred/d3c790c7f92841ed70a3 to your computer and use it in GitHub Desktop.
deep nested property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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