最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you extract content out of object? - Stack Overflow

programmeradmin1浏览0评论

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 array of arrays?

I am very new to JavaScript, I need to look inside this obj and get the text out of. I would really appreciate any insight.

I have used console.log(data), and I am seeing this output. I need to get rid of \ from this output. I tried:

data.strReplace(/\\/g/,'');

and it did not work.

The data looks like this:

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

Here's a screenshot of what I'm seeing:

This is the console output:

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 array of arrays?

I am very new to JavaScript, I need to look inside this obj and get the text out of. I would really appreciate any insight.

I have used console.log(data), and I am seeing this output. I need to get rid of \ from this output. I tried:

data.strReplace(/\\/g/,'');

and it did not work.

The data looks like this:

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

Here's a screenshot of what I'm seeing:

This is the console output:

Share Improve this question edited Aug 11, 2014 at 19:03 user1471980 asked Aug 11, 2014 at 17:48 user1471980user1471980 10.7k49 gold badges147 silver badges246 bronze badges 4
  • 3 that is the string representation of a javascript object.its equivalent to saying obj.toString() – Prabhu Murthy Commented Aug 11, 2014 at 17:48
  • 4 Problem #1: document.write -- You'll have much more success if you log it to the console, after all, it's there for the developer anyways. – ndugger Commented Aug 11, 2014 at 17:49
  • Use console.log(obj) or alert(JSON.stringify(obj, null, 4)) – Bergi Commented Aug 11, 2014 at 17:50
  • 1 strReplace is not a real method. – ndugger Commented Aug 11, 2014 at 18:07
Add a ment  | 

3 Answers 3

Reset to default 10

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:


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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论