So I am new to programming and I have this project that need to be done and I have to use these codes in my web interface. However I am unable to find what function to use to display these codes with the movies heading. Anybody can help please?
This is the code that I need to use. But idk how to display it. I have to make it sortable by year afterwards.
let movieData = {
"The Darjeeling Limited": {
plot: "A year after their father's funeral, three brothers travel across India by train in an attempt to bond with each other.",
cast: ["Jason Schwartzman", "Owen Wilson", "Adrien Brody"],
runtime: 151,
rating: 7.2,
year: 2007,
},
"The Royal Tenenbaums": {
plot: "The eccentric members of a dysfunctional family reluctantly gather under the same roof for various reasons",
rating: 7.6,
year: 2001,
cast: ["Gene Hackman", "Gwnyeth Paltrow", "Anjelica Huston"],
runtime: 170,
},
"Fantastic Mr. Fox": {
year: 2009,
plot: "An urbane fox cannot resist returning to his farm raiding ways and then must help his community survive the farmers' retaliation.",
cast: [
"George Clooney",
"Meryl Streep",
"Bill Murray",
"Jason Schwartzman",
],
runtime: 147,
rating: 7.9,
},
"The Grand Budapest Hotel": {
rating: 8.1,
runtime: 159,
year: 2014,
plot: "A writer encounters the owner of an aging high-class hotel, who tells him of his early years serving as a lobby boy in the hotel's glorious years under an exceptional concierge.",
cast: ["Ralph Fiennes", "F. Murray Abraham", "Mathieu Amalric"],
},
};
2
Answers
To display the movie data on your web interface, you can use JavaScript to access the HTML document and manipulate the DOM (Document Object Model). Here’s an example of how you can create a table to display the movie data:
javascript
This code creates a table element in the HTML document and populates it with the movie data from the movieData object. The for…in loop iterates over each movie in the object and creates a new row in the table for each movie, with cells for the title, year, and rating properties. You can customize this code to display more properties and add sorting functionality as needed.
You can use a for-in loop to iterate over the keys of your
movieData
object, then get the values of the child object withmovieData[key]
, this selects the value frommovieData
where the key iskey
(which appears to be the movie title).See this question for how to select an object by key name: Javascript get object key name