Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. function getDeep(o, p2, p3, p4, p5) {
  2. if (p5 !== undefined) {
  3. if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3) && o[p2][p3].hasOwnProperty(p4) && o[p2][p3][p4].hasOwnProperty(p5)) {
  4. return o[p2][p3][p4][p5];
  5. }
  6. } else if (p4 !== undefined) {
  7. if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3) && o[p2][p3].hasOwnProperty(p4)) {
  8. return o[p2][p3][p4];
  9. }
  10. } else if (p3 !== undefined) {
  11. if (o.hasOwnProperty(p2) && o[p2].hasOwnProperty(p3)) {
  12. return o[p2][p3];
  13. }
  14. } else if (p2 !== undefined) {
  15. if (o.hasOwnProperty(p2)) {
  16. return o[p2];
  17. }
  18. } else if (o !== undefined) {
  19. return o;
  20. }
  21. }
  22. window.onload = function () {
  23. var obj = {a:{b:{c:1}}};
  24. window.console.log(getDeep(obj, 'a', 'b', 'c')); // returns 1;
  25. window.console.log(getDeep(obj, 'a', 'b', 'd')); // returns undefined;
  26. }
  27.  
  28. function getDeep() {
  29. return [].reduce.call(arguments, function(obj, prop) {
  30. return obj && obj[prop];
  31. });
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement