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

140
Visualizações
Fix appended string

I have this code where First Name - Last Name from a textarea will be split and swap places - output will be Last Name - First letter of First name and separated by a "/". However, I have ran out of idea on how to remove the extra "/" at the end of the output.

Input:

John Doe

Jane Doe

Johnny Doe

Desired output: Doe, J / Doe, J / Doe, J

But getting : Doe, J / Doe, J / Doe, J / (with this extra /)

How do I omit this if it is the last?

splitName.onclick = function() {
  document.getElementById("result").innerHTML = '';
  var value = document.getElementById("fullName").value;
  value.split('\n').forEach(fullname => {

    var spaceIndex = fullname.indexOf(" ");
    var firstname;
    var lastname;
        var tournament = "Round # Sixshooter (Tournament Name YYYY):";
    if (spaceIndex == -1) {
      lastname = fullname;
      lastname = "";
    } else {
      firstname = fullname.substring(0, spaceIndex);
      lastname = fullname.substr(spaceIndex + 1);       
    }
       document.getElementById("result").innerHTML += [lastname, firstname[0]].filter(Boolean).join(", ") + " / ";
     
  });
};

<div>
  <textarea cols="50" rows="5" id="fullName" class= ""></textarea>
</div>

<button id="splitName">Click me</button>
<div>
  <br>
</div>
<div class= "border" id="result"></div>

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

0

You can create an array of names and then use Array.join('/')

Example of concatenation with Array.join:

let names = ['Doe, J', 'Doe, J', 'Doe, J'];
document.getElementById("result").innerHTML = names.join(' / ');

will result in

'Doe, J / Doe, J / Doe, J'
about 4 years ago · Juan Pablo Isaza Relatório

0

Using a regular expression might be a little easier. match on the first and last names, and then map over each element in the array the match finds, and then join the array up.

const ta = document.querySelector('textarea');
const regex = /([A-Za-z]+) ([A-Za-z]+)/g;
const arr = ta.value.match(regex);

const mapped = arr.map(el => {
  const [first, last] = el.split(' ');
  return `${last} ${first[0]}`;
});

console.log(mapped.join(' / '));
<textarea>
John Doe

Jane Doe

Billy Joel

Johnny Doe
</textarea>

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