Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kyle Vassella
9,033 PointsHow to return multiple properties of an object within an array?
I'm working on the first project of the Tech Degree, which involves generating a random quote. My code so far:
var randomNumber = Math.floor(Math.random() * 6 );
var quotes = [
{quote: "A man away from home need feel no shame.", source: "Mozart"},
{quote: "Don't train alone - it only embeds your errors.", source: "Giralt of Rivia", citation: "The Witcher 3"},
{quote: "Perseverance is failing 19 times and succeeding the 20th.", source: "Julie Andrews"},
{quote: "That's the thing that you see about great business owners..is this...undying optimism. They're realistic, but they're optimistic. Ooh, that rhymes!", source: "Robert Herjavec", citation: "Shark Tank"},
{quote: "You test people under pressure. And under pressure, he was a dud.", source: "Donald J. Trump", citation: "The Apprentice"},
{quote: "Why are dingleberries brown? That's just the way shit is.", source: "Abraham", citation: "The Walking Dead"}
];
function getRandomQuote () {
return quotes[randomNumber].quote;
};
console.log(getRandomQuote());
I'm needing help on my getRandomQuote(); function. Specifically, my return statement. I'm currently able to return the .quote property but can't think of a way to also return the .source property - let alone the .citation property which only exists on some of my objects.
I'm sure I'm missing something very easy. I tried using a for in loop to access each property of each object, but I don't think it will work given that my objects are inside an array. Is there another way, perhaps in dot notation? Or maybe a for in loop is what I need after all?
~Kyle
3 Answers
Martin Cornejo Saavedra
18,132 PointsYou can concatenate the source to the string you are returning, like this:
function getRandomQuote () {
return quotes[randomNumber].quote + " by " + quotes[randomNumber].source;
};
Daniel Cudney
4,515 Pointsreturn quotes[randomNumber].source;
Daniel Cudney
4,515 Pointsmaybe .source is a cmd, in which case change source to author in your objects
Kyle Vassella
9,033 PointsEDIT: I did not see all of your responses when I wrote this 'for in loop' post. I will look through them all now, thank you for the help!
I think I may have found a way to accomplish what I want using a For In Loop. The following code seems to work:
for (var propName in quotes[randomNumber]){
console.log (quotes[randomNumber][propName]);
}
It just took a little extra thinking to come up with the object name since objects are 'un-named' when inside an array. The object name I chose turned out to be quotes[randomNumber] which selects an random item from the array, all of which are objects.
Kyle Vassella
9,033 PointsKyle Vassella
9,033 PointsThank you! I had thought of this but never tested it (mistake), because in my head I though it would not work. I figured the quotes[randomnNumber].source; would generate yet another randomNumber, thus not guaranteeing my quotes and sources to match.
Why is it that this way works? Somehow, the randomNumber variable is staying consistent and only running once despite it being called twice.
Daniel Cudney
4,515 PointsDaniel Cudney
4,515 Pointsbecause javascript runs from top to bottom and is set already.
so it equals a number and stays set until the code is ran again