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

426
Vistas
Calling a method from a nested object in an array (javascript)

EDIT: Took out the unnecessary bit

Not sure why i can't call this method - I get an error

"textNodes[1].options.aaah" is not a function:

  

  const textNodes = [{
    id: 1,
    room_id: 1,
    text: '"ZZZZzzzzZZZZZZzzzzzzzzzzZZZZZZZZZZZZZ"',

    options: [{
      text: "Wake up",
      setfadeMode: {
        fade: 2
      },
      nextText: 2,
      ahhh() {
        console.log("ahhh");
      },
    }, ],
  }, ];

  textNodes[1].options.ahhh();

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

0

Your intention of getting the object with id: 1 will not work with textNodes[1] since textNodes is an array with just one element. The object in question is valid at textNodes[0] because of this.

Then, once you resolve this, the textNodes[0].options.ahhh() line will still fail because the options property is also an array. You want to reference the first (and only) element the same way: textNodes[0].options[0].ahh(). This will print ahhh to the console as intended.

See the result below for verification of this solution.

const textNodes = [{
  id: 1,
  room_id: 1,
  text: '"ZZZZzzzzZZZZZZzzzzzzzzzzZZZZZZZZZZZZZ"',

  options: [{
    text: "Wake up",
    setfadeMode: {
      fade: 2
    },
    nextText: 2,
    ahhh() {
      console.log("ahhh");
    },
  }, ],
}, ];

textNodes[0].options[0].ahhh();

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