-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

Atif
  • 821
  • 2
  • 10
  • 15
  • 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]" – James Thorpe Jun 16 '15 at 15:48
  • 1
    It's just console formatting. Try: `console.log('family: '); console.log(family[0]);` – tymeJV Jun 16 '15 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 – Bergi Jun 16 '15 at 15:48
  • 1
    Try `console.log('family: ', family[0]);` instead of `console.log('family: '+family[0]);` – CD.. Jun 16 '15 at 15:48
  • 1
    This one may explain: http://stackoverflow.com/questions/14597246/javascript-console-logobject-vs-concatenating-string – saif Jun 16 '15 at 15:55
  • Whats with down voting a question. I dont understand. I spent 2 hours trying to figure out the problem before putting it here. – Atif Jun 16 '15 at 16:04

2 Answers2

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]);
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
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
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 1
    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. – Atif Jun 16 '15 at 15:57