Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

70
Vistas
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')
7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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
})

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos