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

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

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 Relatório

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 Relatório

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 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