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

256
Visualizações
javascript [1,2,3,4,5,6,7] becomes 123, 234, 345, 456 etc

i like to ask for advise how to let this array [1,2,3,4, 5, 6, 7] to become 123, 234, 345, 456, 567?

this initial is a number 1234567, i split them

const num = 1234567;
const result = String(num)
  .split("")
  .map((n, i, src) => (src.length !== i + 1 ? `${n}${src[i + 1]}` : null))
  .filter(Boolean)
  .map(Number);

console.log(result)

the result got 12, 23, 34, 45 etc.

how to make it become 123, 234, 345, 456?

thank you for help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The simplest solution will be to loop over the array and loop until length - 2 as:

const num = 1234567;
const strNum = String(num);
const result = [];

for (let i = 0; i < strNum.length - 2; ++i) {
  result.push(strNum.slice(i, i + 3));
}
console.log(result);

You can even create a generic function and get the result string of desired length

const num = 1234567;
const strNum = String(num);

function getPartialString(str, len) {
  const result = [];
  for (let i = 0; i < strNum.length - (len - 1); ++i) {
    result.push(strNum.slice(i, i + len));
  }
  return result;
}
console.log(getPartialString(strNum, 3));
console.log(getPartialString(strNum, 4));
console.log(getPartialString(strNum, 5));
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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