Skip to content Skip to sidebar Skip to footer

How Do You Extract Content Out Of Object?

I am trying to get data remotely via AJAX. When I look at the data via document.write(obj), I get this on my browser: [object Object] What does this mean? Is this an array or arra

Solution 1:

Don't use document.write() for debugging.

Use console.log() and check your browser's developer tools.

[object Object] is the way that an object is displayed as a string: you're seeing that because you're displaying the data as HTML. You'd also see that with alert(), which is another thing that people mistakenly use for debugging.

The console will show you the actual piece of data, like this:

Console view of a JavaScript object


Regarding your new information: there are a few things wrong here.

  1. The backslashes are there for a reason: they escape the quotation marks that form the string. The only reason you'd want to remove them is to convert the string to an object (which is what I assume you're trying to do.
  2. You've removed a quotation mark in-between the last closing curly brace and the last closing bracket. It should be

    ["{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092]]"}
    

    This is what you have:

    ["{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092]]}
    

    (Look at the difference in syntax highlighting.)

  3. Your regular expression has a typo: it should be /\\/g, not /\\/g/.

  4. The string replace method is .replace(), not .strReplace().
  5. Even if you fix all of that, your "object" is still a string (well, technically that's an object in JavaScript, but you know what I mean). To get an object from that, use JSON.parse().

Once you fix all of that, it works fine.

Demo

Solution 2:

There's an addon for Firefox called Firebug. This will allow you to expand objects, to look at their structure and contents.

For your question on how to extract the data, you can for example get the name from a person object by doing something like this:

var name = person.name;
console.log(name);

This will get the name of the person object that was created. Now adjust this to whatever your object name is, and whatever information you are looking to extract. As I said firebug will make things easier to be able to visually see the structure of the object.

Solution 3:

Chrome DevTools or FireFox FireBug are your best friend. Open that up, and then use console.log(data) to see it in the tool's console.

Post a Comment for "How Do You Extract Content Out Of Object?"