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

197
Visualizações
How to split a string input that uses the same separator for items and their values

I've a text msg in which I've to format the values based on the time and then save it to the excel file. I tried with spilt and join function but it has new line so its not working.Is there any way to do it in javascript?

mytext:

16:15 test1 success
17:05 test4 success
17:40 test4234 down
18:25 test322 success
19:10 test423 success
20:20 test123 down
21:05 test454 success
21:40 test4 success
22:40 test145 success
23:35 test1123 down
00:35 testxx success
20:20 test123 down
14:20 test126 down
13:20 test177 success

mycode.js

const data =
'16:15 EURJPY-OTC PUT
17:05 USDJPY-OTC PUT
17:40 EURJPY-OTC PUT
18:25 EURGBP-OTC CALL
19:10 USDJPY-OTC PUT
20:20 USDJPY-OTC PUT
21:05 NZDUSD-OTC CALL
21:40 EURUSD-OTC CALL
22:40 USDJPY-OTC PUT
23:35 USDJPY-OTC PUT
00:35 NZDUSD-OTC CALL';
console.log(data.split('').join(','),'datataaaaa')
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assuming that the received data has the same delimiter for seprating entries and for separating fields (of each entry) you can still parse the input reliably because you know that each entry always has 3 fields.

function parseEntries(data) {
  const entries = []

  const tokens = data.split(' ') // space is the delimiter 
  for (let i = 2; i < tokens.length; i += 3) {
    entries.push({
      time: tokens[i - 2],
      name: tokens[i - 1],
      status: tokens[i]
    })
  }

  return entries
}

const data =
  '16:15 EURJPY-OTC PUT 17:05 USDJPY-OTC PUT 17:40 EURJPY-OTC PUT 18:25 EURGBP-OTC CALL'

const entries = parseEntries(data)
console.log(entries)

entries.forEach(e => {
  // do whatever you need here with each entry
  // e.time
  // e.name
  // e.status
})

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