I am creating a page that will filter some suggestions, where the user can vote for the ones that are most interesting to him.
I'm dividing this screen into 3 tabs [Top Rated], [Newest] and [My Votes] When starting the screen I make a call in the database that brings all the suggestions, I do it with 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 votes on a suggestion, I reorder the arrays according to their respective tab [Top Rated], [Newest] and [My Votes]
this.moreVoted and this.moreNew arrays are reordered without any problem. However I need to perform other actions with the array this.meusVotes besides simply reordering, I need to check if it removed a vote, if I have done this I need to remove this position from the array and then show it again to the user
To remove a position from the array I am doing the following:
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. Just for testing I did as follows:
this.meusVotos.shift()
console.log(this.meusVotos)
But the result is the same, no elements are removed from the array. The most curious thing is that I made a copy of these arrays to test on the browser console and it works without errors. The following example works fine on the console:
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"
}
]
for loop 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 welcome
.filter()array method. array.filterx < lengthtox < this.meusVotos.lengthas I understand, length is fixed and you modified the array without getting a new length value. thethis.meusVotos.lengthis a live checked variable and thelengthis static, it stays the same if you modify the array, thus making x out of bounds at some point.this.meusVotos.lengtho resultado ainda é o mesmo