-2

I have a record that contains the following.

var web = [{
            url : "www.facebook.com",  
            content : "Social Media Website." 
           },
           {
            url : "www.reddit.com",
            content : "A vast forum for different topics" 
           }] ;

I am trying to print out the url part of the record by doing the following

for(var i=0;i<web.length;i++)
{
    alert({"url":web[i].url,"description":web[i].content})    
}

but I am getting as an output [object Object].

Any help is appreciated.

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
Johny
  • 129
  • 11
  • 1
    you can't alert an object, you should use console.log to show your object on browser console – LellisMoon Nov 17 '16 at 14:28
  • 1
    What @Mr.George said, but also, spending some time to learn how to use the debugger in your browser's developer tools is time well spent. – Heretic Monkey Nov 17 '16 at 14:30
  • {"url":web[i].url,"description":web[i].content} is an object and you alert this object. Alerts parameter must be a string. – Bünyamin Sarıgül Nov 17 '16 at 14:30
  • If you have an array of objects that all have the same structure, try passing it to `console.table()` - blew my mind when I found out that existed. – Joe Clay Nov 17 '16 at 14:31
  • you are passing an object to alert function change that with a string like so alert("url: " + web[i].url + "description:" + web[i].content) – Aykut Ateş Nov 17 '16 at 14:34
  • So why someone should print and object on alert? Debug an application using alert isn't a nice way – LellisMoon Nov 17 '16 at 14:35

2 Answers2

0

Alert takes a string, not an object. You can get a decent representation of your object as a string with JSON.stringify. Try

alert(JSON.stringify({"url":web[i].url,"description":web[i].content}));
Alejandro C.
  • 3,771
  • 15
  • 18
0

Two different ways:

use console.log to show on browser console your object.

print your object with alert like a string:

alert("{"+
"url: "web[i].url+","+
"description: "web[i].content+
"}");

on first case

web={
  url:'your url',
  content:'blablabla'
};
console.log({"url":web.url,"description":web.content});
LellisMoon
  • 4,810
  • 2
  • 12
  • 24