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

183
Visualizações
Split an array of strings with each string containing more than one space

I have an array of strings like this. Both left and right portions in the string are separated with spaces (More than 1 space for each).

const arr = [
  'A1789   Other tuberculosis of nervous system',
  'A179    Tuberculosis of nervous system, unspecified',
  'A1801   Tuberculosis of spine'
];

I need to turn this into an array of objects like this, with the first portion as the key and the second portion as the value of the key.

const arrOfObj = [
  { A1789: 'Other tuberculosis of nervous system' },
  { A179: 'Tuberculosis of nervous system, unspecified' },
  { A1801: 'Tuberculosis of spine' }
];
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I would split by space, assuming your key cannot contain space. So we'll have first item your key and the "rest" your value, which we can trim

arr.map(s => {
  const [key, ...value] = s.split(" ");
  return { [key]: value.join(" ").trim() }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

I'd think something like this would work:

const arrOfObj = {};
arr.forEach(item => {
    const match = str.match(/^([A-Za-z0-9]+)\s+(.+)/);
    arrOfObj[match[1]] = match[2];
});

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