1

I have a function for picking out the parts of an entered name:

  function getNameParts(list) {
    first_name = list[0];
    middle_name = "";
    last_name = list[1];
    if(list.length > 2) {
      middle_name = list[1];
      last_name = list[2];
    }
    names = {"first": first_name, "middle": middle_name, "last": last_name};
    console.log("names", names)
    return names
  }

The console states that names is a hash inside this function.

But when I use this function like this:

name = getNameParts($("#person_name").val().split(' '));
console.log("name", name)

name is a string "[object Object]"

What's up with this?

2
  • Not sure what you're asking, because [object Object] is what I would expect. names is an object in your code. Commented Jun 14, 2012 at 14:47
  • I'm asking because [object Object] is a string and not a hash object Commented Jun 14, 2012 at 14:55

6 Answers 6

3

Use JSON.stringify()

console.log('names', JSON.stringify(names));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Sign up to request clarification or add additional context in comments.

Comments

1

try this one;

function getNameParts(list) {
    var first_name = list[0];
   var middle_name = "";
    var last_name = list[1];
    if(list.length > 2) {
      middle_name = list[1];
      last_name = list[2];
    }
    var names = {"first": first_name, "middle": middle_name, "last": last_name};
    console.log("names", names)
    return names
  }

/// declare name varible

var name = getNameParts($("#person_name").val().split(' '));
console.log("name", name)

1 Comment

Nice! Yep this was the problem, thanks! I tend to forget to declare my variables, because I mostly program in ruby and coffeescript.
1

Is it really a string? You return an object from your function.

Try the following sample:

name = getNameParts($("#person_name").val().split(' '));
console.log("first", name.first);
console.log("middle", name.middle);
console.log("last", name.last);

You can access each of the properties values. When you try to parse an entire object to string it returns "[object Object]", as expected.

1 Comment

I've done this, just didn't include it in the question. I get undefined for each part of the hash. In my code I even made name into window.name to see what methods I could do to it and all the autocomplete methods are String type methods
0

name is not a string. It is an object so when you are logging this value in console instead of ideally showing you the contents of the object, it is showing it's type which is console.

To verify this, you can log names.first or any other property in it

Comments

0

name is an object, but when you use vendor-specific console.log function, it may try to coerce its arguments to string depending on how this particular environment programmers implemented it. Object's default stringification method is to return [object Object], which is exactly what you get.

Comments

0

Look at this, it seems to be working just fine:

 $(document).ready(function() {
      function getNameParts(list) {
            var first_name = list[0],
                middle_name = "",
                last_name = list[1],
                names;

            if(list.length > 2) {
                middle_name = list[1];
                last_name = list[2];
            }
            names = {"first": first_name, "middle": middle_name, "last": last_name};
            console.log(names)
            return names;
        }

        var testString = "aaa bbb ccc";
        var name = getNameParts(testString.split(' '));
        console.log(name);

        console.log(name.first + " " + name.middle + " " + name.last);

   });

When you do console.log, if you do console.log("name" + names) and names is an object, it will show you the typeof names that is [object Object] instead of it's contents. For strings and numbers is alright to do it like that.

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.