2

So I'm currently building a random quote generator and whenever I try to append values from my object, it just returns:

[object Object]

Here's my code:

$("#quoteBubble").append("<p>" + quotes[Math.floor(Math.random() * quotes.length)] + "</p>");

Also, here's the general layout of my object/array:

var quotes = [{
"quote": "eeyyy sik beets yo.",
"author": "Jamen Marz"
},

If anyone here could help me solve my issue, that would be greatly appreciated :D

Edit: Here's a link to my CodePen - http://codepen.io/Jelani/pen/ojYZaa?editors=101

3 Answers 3

3

You need to access the property yet. Change it to $("#quoteBubble").append("<p>" + quotes[Math.floor(Math.random() * quotes.length)].quote + "</p>"); to access the quote property

or if you really like to get the string-notation of the object use JSON.stringify(...) like

$("#quoteBubble").append("<p>" + JSON.stringify(quotes[Math.floor(Math.random() * quotes.length)]) + "</p>");
Sign up to request clarification or add additional context in comments.

Comments

3

You have an array of objects, so

quotes[Math.floor(Math.random() * quotes.length)]

is going to return one of the objects in that array, like

{
    "quote": "eeyyy sik beets yo.",
    "author": "Jamen Marz"
}

As is, your code is trying to print the object itself, not one of the properties you have defined in it. So you need to specify the property you want to have printed out, ie

quotes[Math.floor(Math.random() * quotes.length)].quote 
//or
quotes[Math.floor(Math.random() * quotes.length)].author

Comments

2

This is because your quote variable is actually an array of objects. Being so, calling quotes[0] brings an object with two properties. Calling quotes[n].quote will bring the quote itself and calling quote[n].author will bring the author from the same quote. (0 < n < quotes.length)

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.