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

  • SEARCH
  • Home
  • 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 8045319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:30:41+00:00 2026-06-05T05:30:41+00:00

Possible Duplicate: javascript test for existence of nested object key I’m attempting to construct

  • 0

Possible Duplicate:
javascript test for existence of nested object key

I’m attempting to construct an error message for a formset by testing if a certain object is not undefined, and if it’s not undefined, then I end up populating it with that error message. The main problem is that I have to validate if each nested object is undefined, which results in some pretty ugly code. Here’s the example:

errorsForField: function(fieldName, formsetName, formNumber) {
            if (typeof this.model.errors != 'undefined'){

                var fieldError = document.createElement('span');
                $(fieldError).addClass('field-error');

                // THE FOLLOWING LINE THROWS ERROR.
                if (formsetName && _.isUndefined(this.model.errors[formsetName][fieldName]) != true) {
                    $(fieldError).text(this.model.errors[formsetname][fieldName]);
                } else if (typeof this.model.errors[fieldName] != "undefined"){
                    $(fieldError).text(this.model.errors[fieldName]);
                }

                this.errors[fieldName] = fieldError.outerHTML;
                return fieldError.outerHTML; 
            }
            return false; 
        },

I get an error stating that I cannot determine [fieldName] of an undefined object this.model.errors[formsetName]. In other words, I have to first determine if this.model.errors[formsetName] is empty and then test if [fieldname] is undefined.

This seems like a really cumbersome solution. Any suggestions for changing this?

  • 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-06-05T05:30:42+00:00Added an answer on June 5, 2026 at 5:30 am

    You can create a library function that takes property names as parameters and returns the final value if it exists, or null:

    function TryGetPropertyValue(o, propertyName1 /*, ... propertyNameN */) {
        var names = [].slice.call(arguments, 1);
        while (o && names.length) {
            o = o[names.shift()];
        }
        return names.length ? null : o;
    }
    

    Call it like:

    var err = TryGetPropertyValue(this.model.errors, formsetName, fieldName) ||
              TryGetPropertyValue(this.model.errors, fieldName);
    if (err != null) {
        $(fieldError).text(err);
    }
    

    If you want it to return undefined instead of null if the field is not found, you can change the function slightly:

    function TryGetPropertyValue(o, propertyName1 /*, ... propertyNameN */) {
        var names = [].slice.call(arguments, 1);
        while (o && names.length) {
            o = o[names.shift()];
        }
        if (names.length == 0) {
            return o;
        }
    }
    

    http://jsfiddle.net/HbggQ/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I test for an empty Javascript object from JSON? Is
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
Possible Duplicate: Javascript - array.contains(obj) What's wrong with this: var zipCodes =(['90001','90002','90003']); Test if
Possible Duplicate: Javascript Object.Watch for all browsers? I just read Mozilla's documentation for the
Possible Duplicate: Finding Variable Type in JavaScript How can I test if a value
Possible Duplicate: Get property of object in JavaScript If I have the following: var
Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }
Possible Duplicate: Javascript window resize event I want to get an event which is
Possible Duplicate: Javascript sleep How to delay execution in between the following in my
Possible Duplicate: javascript hide/show element How to hide or show div on click in

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.