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

355
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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