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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:54:39+00:00 2026-06-05T20:54:39+00:00

I have a variable that may contain objects or may be undefined. I wish

  • 0

I have a variable that may contain objects or may be undefined. I wish to add additional objects to this variable. How do I do that?

code example when applicable:

function(){ 
    var comments;
    if(fbposts[fbpost].comments.count){
        for(var comment in fbposts[fbpost].comments.data){
                comments = ({
                    name: fbposts[fbpost].comments.data[comment].from.name,
                    link: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id,
                    img: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id+"/picture",
                    message: fbposts[fbpost].comments.data[comment].message,
                    created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time)),
                })
            }

    }
    return comments;}(),
  • 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-05T20:54:40+00:00Added an answer on June 5, 2026 at 8:54 pm

    Test if it is undefined and if so assign it to an empty object:

    if (typeof yourVar === "undefined")
        yourVar = {};
    
    yourVar.additionalObject1 = { something : "test" };
    yourVar.additionalObject2 = { something : "else" };
    

    EDIT: OK, now that you’ve added code to your question, it seems like your comments variable should be an array, since you are adding to it in a loop. So I think you’d want to do something like this:

    (function(){ 
        var comments = [];
        if(fbposts[fbpost].comments.count){
            for(var comment in fbposts[fbpost].comments.data){
                    comments.push({
                        name: fbposts[fbpost].comments.data[comment].from.name,
                        link: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id,
                        img: "http://www.facebook.com/"+fbposts[fbpost].comments.data[comment].from.id+"/picture",
                        message: fbposts[fbpost].comments.data[comment].message,
                        created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time)),
                    });
             }    
        }
        return comments;
    })();
    

    comments will thus contain one element for each comment in your source data. If there are no comments it will be an empty array. (If you want it to return undefined if there are no comments then leave the variable declaration where it is as var comments and add comments=[]; just inside the if statement.)

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

Sidebar

Related Questions

Suppose that I have an integer variable in a class, and this variable may
I have strings that contain a variable number of leading hyphens and which may
I have a variable in my xsl that may or may not be declared.
I have a variable that can either contain a list of strings or a
I have a variable that I'm using to build a JavaScript function call, and
I have a variable that has this string: <DIV><SPAN style=FONT-FAMILY: Tahoma; FONT-SIZE: 10pt>[If the
I have three variables that may or may not contain data. let's call them
Using CakePHP 1.3.6 I'm sure I may have to add code and examples, but
I have a javascript file that reads another file which may contain javascript fragments
I have a variable that im define with another variable and some text. $title

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.