Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

425
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!