Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6127705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:32:32+00:00 2026-05-23T16:32:32+00:00

In javascript, lets say I want to access a property deep in an object,

  • 0

In javascript, lets say I want to access a property deep in an object, for example:

entry.mediaGroup[0].contents[0].url

At any point along that structure, a property may be undefined (so mediaGroup may not be set).

What is a simple way to say:

if( entry.mediaGroup[0].contents[0].url ){
   console.log( entry.mediaGroup[0].contents[0].url )
}

without generating an error? This way will generate an undefined error if any point along the way is undefined.

My solution

if(entry) && (entry.mediaGroup) && (entry.MediaGroup[0]) ...snip...){
   console.log(entry.mediaGroup[0].contents[0].url)
}

which is pretty lengthy. I am guessing there must be something more elegant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:32:33+00:00Added an answer on May 23, 2026 at 4:32 pm
    /*decend through an object tree to a specified node, and return it.
      If node is unreachable, return undefined. This should also work with arrays in the tree.                                                                                               
      Examples:                                                                                                                                                                            
        var test1 = {a:{b:{c:{d:1}}}};                                                                                                                                            
        console.log(objectDesend(test1, 'a', 'b', 'c', 'd'));                                                                                                                
        var test2 = {a:{b:{c:1}}};     //will fail to reach d                                                                                                                                         
        console.log(objectDesend(test2, 'a', 'b', 'c', 'd'));
    */
    var objectDescend = function(){
        var obj = arguments[0];
        var keys = arguments;
        var cur = obj;                                                                                                                                                        
        for(var i=1; i<keys.length; i++){                                                                                                                                     
            var key = keys[i];                                                                                                                                                
            var cur = cur[key];                                                                                                                                               
            if(typeof(cur)=='undefined')                                                                                                                                      
                return cur;                                                                                                                                                   
        }                                                                                                                                                                     
        return cur;                                                                                                                                                           
    }                                                                                                                                                                         
    
    var test1 = {a:{b:{c:{d:1}}}};                                                                                                                                            
    console.log(objectDescend(test1, 'a', 'b', 'c', 'd'));                                                                                                                
    var test2 = {a:{b:{c:1}}};                                                                                                                                              
    console.log(objectDescend(test2, 'a', 'b', 'c', 'd'));
    

    So this will return either the value you are looking for, or undefined since that value doesn’t exist. It won’t return false, as that may actually be the value you are looking for (d:false).

    In my code base, I add Object.prototype.descend, so I can do test1.descend(‘a’, ‘b’, ‘c’, ‘d’). This will only work in ECMAScript 5 (IE>=9) since you need to make it so your function doesn’t appear in enumerations. For more info:
    Add a method to Object primative, but not have it come up as a property

    Here is my code for that:

    Object.defineProperty(Object.prototype, 'descend', {
        value: function(){
            var keys = arguments;
            var cur = this;
            for(var i=0; i<keys.length; i++){
                var key = keys[i];
                var cur = cur[key];
                if(typeof(cur)=='undefined')
                    return cur;
            }
            return cur;
        }
    });
    
    
    
    var test1 = {a:{b:{c:{d:false}}}};
    //this will return false, which is the value of d                                                                                   
    console.log(test1.descend('a', 'b', 'c', 'd'));                                                                                                                       
    var test2 = {a:{b:{c:1}}};
    //undefined since we can't reach d.                                                                                                
    console.log(test2.descend(test2, 'a', 'b', 'c', 'd'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For this example lets say I want to validate a google plus profile URL
Lets say we want a library of javascript-based pieces of functionality (I'm thinking jquery):
Is there anyway to grab the referring URL using javascript, lets say the reffering
Let's say using Javascript, I want to match a string that ends with [abcde]*
Lets say you need to attach some JavaScript functionality to an ASP.NET User Control
Lets say I have the following array in JavaScript: var skins = new Array('Light',
Is it possible through javascript to read the number from the iphone. Lets say
Let's say I instantiate an object in Javascript like this: var myObj = new
Hallo all. I got a javascript object with some propeties let's say function Animal()
Lets say I want to replace the following string within a textfile: document.write(unescape(%3Cscript src='

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.