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 8821129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:46:37+00:00 2026-06-14T05:46:37+00:00

I’m using mongodb and ajax calls to retrieve data. When it turns to javascript

  • 0

I’m using mongodb and ajax calls to retrieve data. When it turns to javascript object, the properties that I use to generate html sometimes don’t exist.
Look at this call:

$.ajax({
        url: 'api/v1/mention/'+id,
        type: "GET",
        dataType: "json",
        data : {login : "demo"},
        success: function(mention) {
            display_mention_text(mention.texto);
        }
    });

In this case i’m calling mention.texto, but could be mention.picture or any properties. Sometimes it is undefined and crashes the app.

This method calls a property from a object and if its undefined , return an empty string.
Some examples for calling this method(the first one is an object, the other are properties):

get_property(mention,"text")
get_property(mention,"user","name")
get_property(mention,"picture")

The method is defined as follows:

function get_property(obj){
    var args = Array.prototype.slice.call(arguments),
     obj = args.shift();
    if (checkNested(obj,args)) {
       //what should I do here?
    } else{
                   //the property is undefined and returns ""
        "";
    };
}


    //check if a object has N levels of propertys
function checkNested(obj /*, level1, level2, ... levelN*/) {
  var args = Array.prototype.slice.call(arguments),
      obj = args.shift();

  for (var i = 0; i < args.length; i++) {
    if (!obj.hasOwnProperty(args[i])) {
      return false;
    }
    obj = obj[args[i]];
  }
  return true;
}

In the first method get_property, if the property do exist, how do I call that??
I would have the object and his propertys as an array like:
object

params = ["user","name"]

but I can’t call like following:

object.["user","name"]
  • 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-14T05:46:38+00:00Added an answer on June 14, 2026 at 5:46 am

    Replace the if statement in the get_property function with the for loop from the checkNested function. Then instead of returning true or false, return the value found or "".

    function get_property(obj){
        var args = Array.prototype.slice.call(arguments),
            obj = args.shift();
    
         // Here's your 'for' loop. The 'if' statement is gone.
        for (var i = 0; i < args.length; i++) {
            if (!obj.hasOwnProperty(args[i])) {
              return "";   // I changed the value of this 'return' statement
            }
            obj = obj[args[i]];
        }
        return obj;  // I change the value of this 'return' statement
    }
    

    Again, all I did was copy paste your own code from one function to the other, and change the values of the return statements.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a small JavaScript validation script that validates inputs based on Regex. I
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.