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

232
Views
¿Generar matriz con valores A1, A2, A3, B1, B2, B3... etc.?

Estoy tratando de generar una matriz de objetos donde genera una matriz con este aspecto:

 [ { _id: 2, label: 'A1' }, { _id: 3, label: 'A2' }, { _id: 4, label: 'A3' }, { _id: 5, label: 'B1' }, { _id: 6, label: 'B2' }, { _id: 7, label: 'B3' } ]

Y todo el camino hasta la letra "G"... Sin embargo, parece que no puedo entender cómo generar esto automáticamente. Hasta ahora se me ocurrió esto, pero en mi salida se saltan letras y simplemente no se genera correctamente :) Me vendría bien un poco de ayuda en esto.

Lo que tengo hasta ahora:

 function nextChar(c) { return String.fromCharCode(c.charCodeAt(0) + 1); } const supersetOptions = computed(() => { const maxGroup = 6; const maxItems = 12; let defaultLetter = 'a'; return Array.from({ length: maxItems }, (value, index) => { const currentLetter = nextChar(defaultLetter) defaultLetter = nextChar(currentLetter); return { label: `${currentLetter}${index + 1}`, value: `${currentLetter}${index + 1}` }; }); })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Parece que comienza el _id desde 2, pero asumí que se trata de un error tipográfico. Si no, entonces solo i+2 en lugar de i+1 en la línea 8.

También tiene diferentes nombres de propiedad entre lo que dice que quiere y lo que su código intenta producir. Esto coincide con lo que dijiste que querías.

 function generate(count) { const results = [] for(let i = 0 ; i < count ; i++) { const letter = String.fromCharCode(65 + (i / 3)) const delta = (i % 3) + 1 results.push({ _id: i+1, label: `${letter}${delta}` }) } return results } console.log(generate(21))

about 4 years ago · Juan Pablo Isaza Report

0

Puede incrementar la letra después del tamaño de grupo deseado y verificar con el resto.

 const nextChar = c => (parseInt(c, 36) + 1).toString(36), computed = () => { const maxPerGroup = 3, maxItems = 12; let letter = 'a'; return Array.from({ length: maxItems }, (_, index) => { const value = `${letter}${index % maxPerGroup + 1}`; index++; if (index % maxPerGroup === 0) letter = nextChar(letter); return { index, value }; }); } console.log(computed());
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Usando generador

 function* labels(end) { for (let i = 0; i <= end; i += 1) { yield { _id: i + 1, label: `${String.fromCharCode(65 + (Math.floor(i / 3) % 26))}${i % 3 + 1}` } } } for (const item of labels(122)) { console.log([item._id, item.label]) }

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!