Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getDeep(o, p2, p3, p4, p5) {
- if (p5 !== undefined) {
- if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3) && o[p2][p3].hasOwnProperty(p4) && o[p2][p3][p4].hasOwnProperty(p5)) {
- return o[p2][p3][p4][p5];
- }
- } else if (p4 !== undefined) {
- if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3) && o[p2][p3].hasOwnProperty(p4)) {
- return o[p2][p3][p4];
- }
- } else if (p3 !== undefined) {
- if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3)) {
- return o[p2][p3];
- }
- } else if (p2 !== undefined) {
- if (o.hasOwnProperty(p2)) {
- return o[p2];
- }
- } else if (o !== undefined) {
- return o;
- }
- }
- window.onload = function () {
- var obj = {a:{b:{c:1}}};
- window.console.log(getDeep(obj, 'a', 'b', 'c')); // returns 1;
- window.console.log(getDeep(obj, 'a', 'b', 'd')); // returns undefined;
- }
- function getDeep() {
- return [].reduce.call(arguments, function(obj, prop) {
- return obj && obj[prop];
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement