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

214
Views
llenar el espacio en blanco con elementos de la matriz

Quiero llenar el '_' con una matriz de referencia:

 const reference = ['this is', 'a beautiful', 'car']; const blank = ['I know', '_']; console.log(fillBlank(reference)) function fillBlank(reference) { const ref = reference.join(' '); if(blank.length) { return // I know this is a beautiful car } else { return ref; } }

Tenga en cuenta que podemos tener espacios en blanco en diferentes lugares, pero solo tenemos uno en cada matriz en blanco:

 const blank = ['I know', '_']; const blank = ['_', 'I know']; const blank = ['I know', '_', 'right?'];

¿Cómo puedo hacer esto?

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

0

Puedes mapear y unirte

 const reference = ['this is', 'a beautiful', 'car']; const ref = reference.join(' '); const fillBlank = (blank,ref) => blank.map(item => item === "_" ? ref : item).join(", ") console.log(fillBlank(['I know', '_'],ref)) console.log(fillBlank(['_', 'I know'],ref)) console.log(fillBlank(['I know', '_', 'right?'],ref))

Podemos llevar esto un poco más allá y escribir en mayúsculas la primera letra de la referencia si es necesario.

 const reference = ['this is', 'a beautiful', 'car']; const ref = reference.join(' '); const fillBlank = (blank,ref) => blank.map((item,i) => item === "_" ? i>0? ref : ref.slice(0,1).toUpperCase() + ref.slice(1) : item).join(", ") console.log(fillBlank(['I know', '_'],ref)) console.log(fillBlank(['_', 'I know'],ref)) console.log(fillBlank(['I know', '_', 'right?'],ref))

about 4 years ago · Juan Pablo Isaza Report

0

Prueba esto en lugar de 'return // I know this is a beautiful car':

 return blank.map(item => item === '_' ? ref : item).join(' ')
about 4 years ago · Juan Pablo Isaza Report

0

function fillBlank(arrToReplace, reference) { const concatedRef = reference.join(' '); arrToReplace.splice(arrToReplace.findIndex(el => el === '_'), 1, concatedRef); return arrToReplace.join(' '); }
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!