I created the array of object showing each movie title and rating. However I try to use console.log("You have watched " + movieObject.movieDB[0]);, the property, movieDB does not show, instead it shows "[object Object]". Does anyone know the solution? The code below is exactly what I have done.
<code>
var movieObject = {
movieDB: [
{title: "In Brudges", rating: "- 5 stars"},
{title: "Frozen", rating: "- 4.5 stars"},
{title: "Mad Maz Fury Road", rating: "-5 stars"}
]
};
console.log("You have watched " + movieObject.movieDB[0]);
console.log("You have not seen " + movieObject.movieDB[1]);
console.log("You have seen" + movieObject.movieDB[2]);
console.log("You have not seen " + movieObject.movieDB[3]);
</code>
Each movieDB
object in the movieObject
array is its own object. JavaScript by default won't log it to the console as-is. Instead, you probably want to display the movie's title. To do that, reference the property: movieObject.movieDB[0].title
. That will print the title of the movieDB
object.
Try This. When you use concat operator with string javascript convert object to string, that' why you see [Object Object] in console. Pass the object as the second argument of console function.
Working code
var movieObject = { movieDB: [{ title: "In Brudges", rating: "- 5 stars" }, { title: "Frozen", rating: "- 4.5 stars" }, { title: "Mad Maz Fury Road", rating: "-5 stars" }] }; console.log("You have watched ", movieObject.movieDB[0]); console.log("You have not seen ", movieObject.movieDB[1]); console.log("You have seen", movieObject.movieDB[2]); console.log("You have not seen ", movieObject.movieDB[3]);
因为你通过movieObject.movieDB[0]
访问一个对象,如果你想像console.log("You have watched " + movieObject.movieDB[0].title)")
一样获取这个对象的 title 属性
to print complete object you have to write following code console.log("You have watched " +JSON.stringify(movieObject.movieDB[0]));
To access title or rating you have to write following code: console.log("You have watched " +JSON.stringify(movieObject.movieDB[0].title));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.