Skip to content

Instantly share code, notes, and snippets.

@Yaob1990
Created September 29, 2018 23:23
Show Gist options
  • Select an option

  • Save Yaob1990/851a2741c0f71fa332e427943cf2c996 to your computer and use it in GitHub Desktop.

Select an option

Save Yaob1990/851a2741c0f71fa332e427943cf2c996 to your computer and use it in GitHub Desktop.
function getObjectVal(obj, keyArray, index) {
if (!Object.prototype.toString.call(obj) === "[object Array]") return null;
if (!keyArray) return null;
if (arguments.length === 2) {
return getObjectVal(obj, keyArray, 0);
}
var key = keyArray[index];
if (keyArray.length - index === 1) {
if (!key) return null;
return obj[key];
}
if (!key) return null;
if (!obj[key]) return null;
return getObjectValByKeyArr(obj[key], keyArray, index + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment