Consider this compound object literal:
persons = {
ID1084: { firstName: 'Alice', lastName: 'Liddel' },
ID1382: { firstName: 'Wayne', lastName: 'Campbell' }
};
- Print the entire persons object.
- Print the person with key ID1084.
- Print the last name of the person with key ID1382.
- Send the first and last name with key=ID1382 to the
index.ejs view as the local variables
first and last. Put them in a table that looks
like this:
First Name | Last Name |
Alice | Liddel |
Wayne | Campbell |
Answer:
-------------------------------------------
// Source code file: main.js
var persons = {
ID1084: { firstName: 'Alice', lastName: 'Liddel' },
ID1382: { firstName: 'Wayne', lastName: 'Campbell' }
};
// Write this information to the Node console.
console.log(persons);
console.log(persons.ID1084);
console.log(persons.ID1382.lastName);
// Display the first and last names of the
// person with ID=ID1382 in a table.
const express = require("express");
const app = express( );
app.set("view engine", "ejs");
app.get("/person_names", (req, res) => {
res.render("person_names",
{ first: persons.ID1382.firstName,
last: persons.ID1382.lastName });
});
app.listen(3000, ( ) => {
console.log("Server started");
});
-------------------------------------------
<!DOCTYPE html>
<!-- Source code file: view/person_names.ejs -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Person Names </title>
</head>
<body>
<h1>Person Names</h1>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td><%= first %></td>
<td><%= last %></td>
</tr>
</table>
</body>
</html>