Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Generate array with values A1, A2, A3, B1, B2, B3... etc?

I'm trying to generate an array of objects where it outputs an array looking like this:

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

And all the way up to letter "G"... However, I cant quite to seem to wrap my head around how to generate this automatically. So far I've come up with this, but in my output it's skipping letters, and just isn't generating correctly :) Could use some help on this.

What I have so far:

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 Respostas
Responde à pergunta

0

You appear to start the _id from 2, but I've assumed this is a typo. If not, then just i+2 instead of i+1 on line 8.

You also have different property names between what you say you want, and what your code is attempting to produce. This matches what you said you wanted.

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 Relatório

0

You could increment the letter after the wanted group size and check with remainder.

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 Relatório

0

Using generator

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda