Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Unable to access key value pairs in js object

Unable to access key value pairs in js object. I'm trying to access current day from dayName but I'm unable to do so. It is showing undefined when I'm trying to access the dayName via dayNum key.

JS

app.get("/", (req, res) => {       
    let dayName = {
        "1" : "Monday",
        "2" : "Tuesday",
        "3" : "Wednesday",
        "4" : "Thursday",
        "5" : "Friday",
        "6" : "Saturday",
        "0" : "Sunday"
    }
    
    let today = new Date();
        
    let dayNum = today.getDay().toString();
    console.log(dayName.dayNum);

    res.render("list", {
        day : dayName.dayNum
    });
});

EJS

<!DOCTYPE html>
<html lang="en">
    
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>To Do List</title>
</head>
    
<body>
    <h1>Today is <%= day %></h1>
</body>
    
</html>

OUTPUT:

console output

browser output

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should use the square bracket notation. With the dot notation dayName.dayNum we are looking for the value with key "dayNum" inside the object and since it does not have any such key, it returns undefined. If we use the bracket notation, dayNum will be evaluated to the correct key before object access. See Dot Notation vs. Bracket Notation

res.render("list", {
    day : dayName[dayNum]
});

Also I would recommend using an array instead of object for dayName

const dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
about 4 years ago · Juan Pablo Isaza Denunciar

0

app.get("/", (req, res) => {       
    let dayName = {
        "1" : "Monday",
        "2" : "Tuesday",
        "3" : "Wednesday",
        "4" : "Thursday",
        "5" : "Friday",
        "6" : "Saturday",
        "0" : "Sunday"
    }
    
    let today = new Date();
        
    let dayNum = today.getDay().toString();
    console.log(dayName.dayNum);

    res.render("list", {
        day : dayName.dayNum
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda