sandraqu
sandraqu

Reputation: 1528

How to see array objects in javascript

This line:

alert("<b>feeds.entries["+j+"] is </b>=>"+feeds.entries[j]+"<br>");

gives this result:

<b>feeds.entries[0] is </b>=>[object Object]<br>

I would like to print the list of objects so that I can use them.

This array is being called as such:

var entry=feeds.entries[i];

and entry.title, prints the title of the image. I guessed at "title", and would like to see all the other objects feeds.entries[] contains, but my alert above, prints "[object Object]". The script is zRSSFeed which is an RSS parser. I'm trying to parse a Menalto Gallery 2 (or g2) RSS feed.

Upvotes: 1

Views: 184

Answers (5)

ashisrai_
ashisrai_

Reputation: 6568

You can't render string and object together as they are different type. do print object separately alert(feeds.entries[j]). I think it is good to use console.log instead of alert for debugging.

Upvotes: 2

qwertymk
qwertymk

Reputation: 35256

function join(obj) {
    var ret = '';
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        ret += i + ': ' + obj[i]
    }
    return ret;
}

Call it like this:

alert("<b>feeds.entries["+j+"] is </b>=>"+join(feeds.entries[j])+"<br>");

DEMO

Upvotes: 1

H&#229;vard
H&#229;vard

Reputation: 10080

Try outputting it as JSON:

alert("<b>feeds.entries["+j+"] is </b>=>"+JSON.stringify(feeds.entries[j])+"<br>");

I don't see why you have the tags in the alert though, as they do nothing.

alert("feeds.entries["+j+"] is =>" + JSON.stringify(feeds.entries[j]));

However, if you are using Chrome, I suggest you simply console.log the object(s) and open up the developer console to view the object.

Upvotes: 1

Remi
Remi

Reputation: 21165

if you mean to use them in the browser console, use

for (var i==0; i<feeds.entries.length; i++){
   console.log(feeds.entries[i]);
}

otherwise, please explain more clearly what you want, and how you want to represent your objects or their attributes.

Upvotes: 1

Paul
Paul

Reputation: 20061

I suggest you download and install Firebug, then read the section on Firebug and logging. It will show you how to log to the console.

I'm also a fan of the YUI 2 Logger which displays the messages in a floating window. Either way, using alert is a sure way to slow down development and drive yourself insane. Development should be more fun than that.

As an aside, take a look at the YAHOO.lang.dump() method which shows what is inside an object. [Object object] isn't very useful as you've discovered.

Upvotes: 1

Related Questions