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

183
Views
Cómo ejecutar una función solo una vez para el mismo argumento

Mi llamada de función se da a continuación:

 await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID)

Supongamos que la función se llama con matchID '1', debería ejecutarse, pero si la función se llama de nuevo con matchId '1' (lo hará en mi caso), no debería ejecutarse. Sin embargo, si se llama con id '2' (básicamente id !== '1'), debería ejecutarse. No me importan los jardineros Un argumento.

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

0

Puede rastrear todos los argumentos pasados en una matriz fuera de la función. Cuando llame a la función, verificará si el argumento proporcionado está en la matriz. Si no es así, llame a la función e inserte el argumento en la matriz. Si el argumento ya está en la matriz, no llame a la función.

 const suppliedMatchIDs = []; function insertingMatchIdsInAllTeamPlayers(fieldersA, matchID) { if (suppliedMatchIDs.includes(matchID)) { return; } else { suppliedMatchIDs.push(matchID); } // Your function here }

El concepto general de almacenamiento en caché de argumentos para acelerar las llamadas a funciones se llama memoización .

about 4 years ago · Juan Pablo Isaza Report

0

const matchIdFirstTimeOne = true if(matchId === 1 && matchIdFirstTimeOne) { await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID); matchIFirstTimeOne = false }
about 4 years ago · Juan Pablo Isaza Report

0

Usar el cierre podría resolverlo.

 var insertingMatchIdsInAllTeamPlayers = (function() { var executed = []; return function(fieldersA,val) { if (executed.indexOf(val) == -1) { executed.push(val); console.log(val); } }; })(); insertingMatchIdsInAllTeamPlayers('',1); // console.log(1) insertingMatchIdsInAllTeamPlayers('',1); // insertingMatchIdsInAllTeamPlayers('',2); // console.log(2) insertingMatchIdsInAllTeamPlayers('',2); // insertingMatchIdsInAllTeamPlayers('',3); // console.log(3)
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!