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

74
Views
El primer argumento de Javascript se ignora en funciones anidadas con argumentos extendidos

Tengo esta función variádica realmente simple que recorre sus argumentos y les agrega una cadena "Transformada".

 function Transform(...children) { return children.map(child=> child + " Transformed") } document.write( Transform( Transform(" test1", " test2"), //only test2 gets double transformed Transform(" test3") //here the first argument gets double transformed though ) )

Por alguna extraña razón, al anidarlos, si solo tengo 1 argumento, actúa como se esperaba, pero si tengo más de uno, no afectará el primer argumento.

¿Cómo puedo hacer que la función agregue la cadena Transformed al final de todos los argumentos? (Puede ver que test1 no tiene dos Transformed después, pero test2 y test3 sí).

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

0

También debe volver a distribuir la matriz cuando llame a la función (mire los argumentos del Transform externo):

 function Transform(...children) { return children.map(child=> child + " Transformed") } document.write( Transform( ...Transform(" test1", " test2"), //only test2 gets double transformed ...Transform(" test3") //here the first argument gets double transformed though ) )

Si desea que la función maneje ambos casos, simplemente verifique si cada elemento es una matriz o no, y si el elemento es una matriz, manéjelo también con Transform :

 function Transform(...children) { return children.map(child => Array.isArray(child) ? Transform(...child) : child + " Transformed"); } document.write( Transform( Transform(" test1", " test2"), //only test2 gets double transformed Transform(" test3") //here the first argument gets double transformed though ) )

Versión un poco más detallada:

 function Transform(...children) { return children.map(child => { if (Array.isArray(child)) return Transform(...child) else return child + " Transformed"; }); } document.write( Transform( Transform(" test1", " test2"), //only test2 gets double transformed Transform(" test3") //here the first argument gets double transformed though ) )

about 4 years ago · Juan Pablo Isaza Report

0

 function Transform(...children) { return children.map(child=> child + " Transformed") } document.write( Transform( ...Transform("test1", "test2"), //only test2 gets double transformed Transform("test3") //here the first argument gets double transformed though ) )

Ok, acabo de descubrir que tenía que distribuir la matriz devuelta por la Transformación interna para aplicar la transformación en cada elemento. ¿Crees que hay una mejor manera de lograr el mismo resultado de una manera más intuitiva?

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!