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

231
Views
Repetir elementos en una matriz según la longitud de otra matriz

Tengo dos matrices de cadenas con longitud dinámica, por ejemplo:

 const categories = ['apple', 'pear', 'melon', 'lemon'] const colors = ['red', 'blue', 'green']

y me gustaría que colors.lenght >= categories.lenght . El elemento faltante de colors debe repetirse. Ejemplo:

 const categories = ['apple', 'pear', 'melon', 'lemon'] const colors = ['red', 'blue', 'green', 'red'] // colors[3] = colors[0]
 const categories = ['apple', 'pear', 'melon', 'lemon', 'strawberry', 'orange'] const colors = ['red', 'blue', 'green', 'red', 'blue', 'green']
 const categories = ['apple', 'pear', 'melon', 'lemon', 'strawberry', 'orange', 'cherry'] const colors = ['red', 'blue', 'green', 'red', 'blue', 'green', 'red']

¿Cómo puedo hacer eso?

Comienzo a crear esta función pero no sé cómo repetir los colores de la manera correcta:

 function adjustLenghts(categories: string[], colors: string[]) { const overflowingCategoriesItems = domain.slice(colors.length, categories.length) const repeatedRange = ?? const newColors = ?? return newColors }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

puedes hacer algo como esto

 const categories = ['apple', 'pear', 'melon', 'lemon', 'banana'] const colors = ['red', 'blue', 'green'] const adjustedColor = categories.map((_, i) => colors[i % colors.length]) console.log(adjustedColor)

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!