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

354
Views
Can I set a key-value of a object, a call for a function? If so, how can I call it?

I have an array of objects and for each object I assign an ID and a task to do, as the following;

const Data = [{
        id: 1,
        task: doSomething()
    },
    {
        id: 2,
        task: doSomethingElse()
    },
    {
        id: 3,
        task: doAnotherThing()
    },
    {
        id: 4,
        task: DoYetAnotherThing()
    },
]

I have a for loop which goes over each of the objects in the array and compares it against the wanted id (which is not permanent). When found, I want the program to run it's task. That's when I run into problems:

let wantedID = 3
for (const key of Data) {
    if (key.id == wantedID) {
        key.task
    }
}

I thought that if I just mention key.task it would call the function and the program would run the task, as key.task is a call for the function, but it doesn't work. trying key.task() threw an error, as key.task is not a function..?

How can I fix this?

Thanks!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Remove the () from the object and add them in the for loop. Also define the functions themselves

const Data = [{
        id: 1,
        task: doSomething
    },
    {
        id: 2,
        task: doSomethingElse
    },
]

let wantedID = 2
for (const key of Data) {
    if (key.id == wantedID) {
        key.task()
    }
}
function doSomething(){
  console.log("doSomething")
}

function doSomethingElse(){
  console.log("doSomethingElse")
}

about 4 years ago · Juan Pablo Isaza Report

0

It occured because your functions got called when you declared the array this is because you had called the functions when you are declaring the array so they got called and in this case the value returned by this function will get assigned in that variable.

check the screenshot below enter image description here

Now to make your function get called you need to assign the function instead of calling that function check the screenshot below enter image description here

now in your code the value returned by the function is assigned to that variable check the below screenshot

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

You need to write the func logic inside the value of the obj

let obj = [
  {objFunc: () => { console.log('Do Something 1') }, text: 'Do Something' },
  {objFunc: () => { console.log('Do Something 2') }, text: 'Do Something Else' }, 
]

for(let i = 0 ; i  < obj.length; i++){
    obj[i].objFunc()
}
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!