-1

I have a weird situation.

I take some values from a form and save it in an object.

I print out the values and it prints fine to the console log. But when I try to access it or print again in next line. It returns empty.

Here is the code.

 var family = {};
    var counter = [];

    counter = document.querySelectorAll(".familyMemberBox input[name='member[]']");

    for(x=0;x<counter.length;x++){
                console.log('for loop');
                family[x] = {};

                member_number = counter[x].value;
                member_type = get_member_type(member_number);
                gender = get_gender(member_number);
                age = get_age(member_number);

                family[x]['type'] = member_type;
                family[x]['Gender'] = gender;
                family[x]['Age'] = age;
    }

    for(x=0;x<counter.length;x++){
        console.log(family[x]);
    }

            console.log('family: '+family[0]);

here is the console output

enter image description here

6
  • 2
    console.log(family[x]); is logging the object. console.log('family: '+family[0]); is logging the string representation of the object, which is (unless you override it) "[object Object]" Commented Jun 16, 2015 at 15:48
  • 1
    It's just console formatting. Try: console.log('family: '); console.log(family[0]); Commented Jun 16, 2015 at 15:48
  • 1
    That's not an empty object, that's just a string for an object. Try console.log("family", family[0]) instead Commented Jun 16, 2015 at 15:48
  • 1
    Try console.log('family: ', family[0]); instead of console.log('family: '+family[0]); Commented Jun 16, 2015 at 15:48
  • 1
    This one may explain: stackoverflow.com/questions/14597246/… Commented Jun 16, 2015 at 15:55

2 Answers 2

1

You're making it a string with this statement: console.log('family: '+family[0]);.

You're implicitly calling .toString() when you concat a string with a plain object. The string version of an object is [object Object]. It's not empty, don't worry.

If you're going to show the string version of an object, you're probably going to want to loop through and print each key and value pair, but that's probably not efficient. If you're dead set on printing family to the console, use a comma:

console.log('family: ', family[0]); // should show two individual statements, not just one coerced string
Sign up to request clarification or add additional context in comments.

1 Comment

wow, seems like rookie mistake. Thanks for the detailed answer. I will use it in a loop. This was just to test if I was reading the values fine.
1

This line: console.log('family: '+family[0]); is coercing family[0] to a string before printing. It doesn't mean that the object is empty.

Try this instead:

console.log('family: ');
console.log(family[0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.