Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

427
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda