3
I am creating a page that will filter some suggestions, where the user can vote on which are most interesting to him.
I am dividing this screen into 3 tabs [Most Voted], [Newest] and [My Votes]
When starting the screen I make a call in the database that brings all suggestions, I do it through the axios
axios
.get("/sugestoes/carregar/xxxx")
.then(res => {
this.sugestoesGeral = res.data.sugestoes
for(var i=0; i < this.sugestoesGeral.length; i++){
if(this.sugestoesGeral[i].meu_voto === "S"){
this.meusVotos.splice(1, 0, this.sugestoesGeral[i])
}
}
this.maisVotados = [...this.sugestoesGeral]
this.maisNovos = [...this.sugestoesGeral]
this.ordernarArrVotos(this.maisVotados)
})
.catch(err => {
console.log(err);
});
This way I start my three different arrays from the first call in the database
When the user Vote on any suggestion I make him reorder the arrays according to their respective [Most Voted] tab, [Newest] and [My Votes]
The arrays this.maisVotados
and this.maisNovos
are reordered without any problem. However the array this.meusVotos
i need to perform other actions besides simply reorder, I need to check if he has removed a vote, if I have done so I need to remove that position from the array and then show it again to the user
To remove a position from the array I am doing as follows:
for(var i=0; i < this.sugestoesGeral.length; i++){
if(this.sugestoesGeral[i].meu_voto === 'N'){
var length = this.meusVotos.length
for(var x=0; x < length; x++){
if(this.meusVotos[x].id_sugestao === this.sugestoesGeral[i].id_sugestao){
this.meusVotos.splice(x, 1)
}
}
}
}
console.log(this.meusVotos)
However this way it has no effect and does not remove any position from my array. I did it as follows for testing only:
this.meusVotos.shift()
console.log(this.meusVotos)
However the result is the same, no element is removed from my array. The curious thing is that I made a copy of these arrays to test in the browser console and it works without errors. follow example only to play on the console and see that it works
Arrays:
sugestoesGeral = [
{
status_atual: "Em votação",
categoria: "Parâmetros",
quantidade_votos: 1,
meu_voto: "S",
id_sugestao: 16,
titulo: "Alerta de Valor Excedente por Cliente no MDF-e",
data_criacao: "29/01/2020",
descricao_resumida: "Alerta de Valor Excedente por Cliente no MDF-e",
motivo_rejeicao: "",
data_rejeicao: "",
data_implementacao: "",
data_previsao_execucao: "",
data_encerramento_votacao: "",
cor: "#b8dbff"
},
{
status_atual: "Em votação",
categoria: "Despesas",
quantidade_votos: 1,
meu_voto: "N",
id_sugestao: 7,
titulo: "Nova coluna Controle de Despesas",
data_criacao: "28/01/2020",
descricao_resumida: "Checar vínculo financeiro da Despesa quando vinculada a uma Nota com Financeiro lançado",
motivo_rejeicao: "",
data_rejeicao: "",
data_implementacao: "",
data_previsao_execucao: "",
data_encerramento_votacao: "",
cor: "#b8dbff"
}
]
meusVotos = [
{
status_atual: "Em votação",
categoria: "Parâmetros",
quantidade_votos: 1,
meu_voto: "S",
id_sugestao: 16,
titulo: "Alerta de Valor Excedente por Cliente no MDF-e",
data_criacao: "29/01/2020",
descricao_resumida: "Alerta de Valor Excedente por Cliente no MDF-e",
motivo_rejeicao: "",
data_rejeicao: "",
data_implementacao: "",
data_previsao_execucao: "",
data_encerramento_votacao: "",
cor: "#b8dbff"
},
{
status_atual: "Em votação",
categoria: "Despesas",
quantidade_votos: 1,
meu_voto: "N",
id_sugestao: 7,
titulo: "Nova coluna Controle de Despesas",
data_criacao: "28/01/2020",
descricao_resumida: "Checar vínculo financeiro da Despesa quando vinculada a uma Nota com Financeiro lançado",
motivo_rejeicao: "",
data_rejeicao: "",
data_implementacao: "",
data_previsao_execucao: "",
data_encerramento_votacao: "",
cor: "#b8dbff"
}
]
To remove the element:
for(var i=0; i < this.sugestoesGeral.length; i++){
if(this.sugestoesGeral[i].meu_voto === 'N'){
var length = this.meusVotos.length
for(var x=0; x < length; x++){
if(this.meusVotos[x].id_sugestao === this.sugestoesGeral[i].id_sugestao){
this.meusVotos.splice(x, 1)
}
}
}
}
Any help will be most welcome
Puts a
console.log("ok");
within theif
where is the linethis.meusVotos.splice(x, 1)
to see if you’re in it.– Sam
Yes @Sam when I remove a vote and click to call this method it go through
console.log('OK')
– Fábio Santos
@Sam has some other suggestion of what he could try, I’ve already made some changes with filter() but also without success
– Fábio Santos
Dude, instead of splice, try to push to see if something is added to the array. Just comment on the line
this.meusVotos.splice(x, 1)
and put under it athis.meusVotos.push("x")
... see if the last item of the array appears"x"
.– Sam