You can use the method String.split()
that "breaks" a string into multiple pieces (an array with N elements), just define which will be the separator.
Ex:
"abc 123 def 456".split(' '); // ['abc', '123', 'def', '456']
In your case, if you applied the above method we would have:
"SEXTA-FEIRA 25 JAN 20H00".split(' ');
// ['SEXTA-FEIRA', '25', 'JAN', '20H00']
From this it is only create the desired variables and ignore the first element.
let result = ['SEXTA-FEIRA', '25', 'JAN', '20H00']
dia = result[1]
mes = result[2]
horario = result[3]
If compatibility is not an issue, you can also use destructuring assignment of Ecmascript2015:
let [_, dia, mes, horario] = ['SEXTA-FEIRA', '25', 'JAN', '20H00']
Putting it all together:
const extractData = stringEvento => {
let [_, dia, mes, hora] = stringEvento.split(' ')
return {dia, mes, hora}
}
Full example:
const extractData = stringEvento => {
let [_, dia, mes, hora] = stringEvento.split(' ')
return {dia, mes, hora}
}
let sessoes = [
{
codEvento: 29,
descricao: "SEXTA-FEIRA 25 JAN 20H00",
atual: true
}, {
codEvento: 30,
descricao: "SÁBADO 26 JAN 21H00",
atual: false
}, {
codEvento: 31,
descricao: "DOMINGO 27 JAN 20H00",
atual: false
}
]
// Atualizar sessoes com a data alterada
sessoes = sessoes.map(evento => {
evento.data = extractData(evento.descricao)
return evento
})
console.log(sessoes)
That takes care of your problem:
var r = dataEvento.split(' ')
then just recover by the indexes:this.data = r[1]; this.mes = r[2]; this.horario = r[3];
– NoobSaibot