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

187
Views
descomprimiendo el parámetro rest (...theArgs) antes de pasarlo a la función

Estoy tratando de llamar a una función requerida el número de veces. Para resolver esto, estoy creando una función que toma parámetros de descanso e itera a través del primer argumento. No puedo descomprimir los argumentos restantes y pasarlos a la función. ¿Hay alguna forma de descomprimirlo y pasarlo a la función como parámetros? A continuación se muestra mi código de trabajo. ¿Hay una mejor manera de hacer que funcione? Gracias por adelantado. Agradezco cualquier ayuda.

 function hello(name) {
 console.log("Hello "+ name);
}

function greet(name,time_of) {
 console.log("Good " + time_of +" " +name);
}

function foo(times,x, ...args) {
 for(i=0;i<times;i++) {
 x(arguments)
 //x(args); //works good for hello() but not for greet(). Doesn't pass them to second argument
 x(args[0],args[1]); //This works but not scalable 
 //args.map((element) => x(element)); 

 }
}

foo(2,hello,"Myname");
foo(3,greet,"Myname","Afternoon");
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simplemente extienda los argumentos de nuevo:

 function hello(name) {
 console.log("Hello " + name);
}

function greet(name, time_of) {
 console.log("Good " + time_of + " " + name);
}

function foo(times, x, ...args) {
 for (i = 0; i < times; i++) {
 x(...args)
 }
}

foo(2, hello, "Myname");
foo(3, greet, "Myname", "Afternoon");

almost 4 years ago · Santiago Trujillo 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!