0
Well I have two forEach
. The first takes the value of a sessionStorage
, that is, the ID of the neighborhood registered by the user. The second returns all registered neighborhoods and their respective IDS.
I wanted to make the neighborhood id of the SessionStorage
is equal to the ID of the district registered in the second forEach
. But if I put the neighborhood on the register comparing the second forEach
it will return 5 times the value of registered neighborhoods. How do I solve this?
var getEnderecoLogado = sessionStorage.getItem("dadosendereco");
$.ajax({
url: urlBase + "areaatendimento",
method: 'GET',
success: function (retorno)
{
parseResult = JSON.parse(getEnderecoLogado);
parseResult.forEach(function (item)
{
bairrocadastrado = item.bairroendereco;
});
retorno.data.forEach(function (item)
{
id_atendimento = item.area_atendimento_id;
id_bairro = item.bairro.bairro_id;
nomecidade = item.bairro.municipio.nome;
nomebairro = item.bairro.nome;
nomeestado = item.bairro.municipio.estado.nome;
valorfrete = item.valor_frete;
valorfrete = valorfrete.replace('.', ',');
if (id_bairro == bairrocadastrado)
{
$('.entregatotal').html("R$ " + valorfrete);
$('.bt-finalizar-01 .finalizar-compra-carrinho').css('display', 'block');
}
else
{
$('.entregatotal').html('R$ 0,00');
resultBairro = "<div class='mensagembairro'><strong>Atenção:</strong> O seu bairro não é atendido por este estabelecimento</div>";
$('.finalizar-compra-carrinho').css('display', 'none');
$('.delivery').css('display', 'none');
$('.bt-finalizar-01').html(resultBairro);
}
});
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
console.log('Erro');
}
});
I mean, I want the Session Storage Neighborhood ID to be the same as the one that comes back in ajax to see if you can calculate freight or not
Could display the contents of the variable
getEnderecoLogado
? And if possible also, the contents ofretorno
ajax.– Mathiasfc
Session Storage: [{"idcliente":"ecca2c6b-cd15-443b-b4db-5a15d39196c5","cependereco":"89400000","logradouroendereco":"rua teste","numeroendereco":"90","complementoendereco":"ewew","referencia":"wewew","estadoendereco":"Santa Catarina","cidadeendereco":"Porto União","bairroendereco":10813}] I will put in the answer the return image
– Felipe Michael da Fonseca
Because the
parseResult.forEach(function (item) { bairrocadastrado = item.bairroendereco; }
That oneforeach
will just take theultimo item.bairroendereco
. I couldn’t figure out what this one was aboutfor
– Isac
Yes but he doesn’t have to take the values from the Sessionstorage array? He doesn’t have to take the last value from the neighborhood, ie the neighborhood ID?
– Felipe Michael da Fonseca
Voce does not need this foreach, Voce can directly pick up 'parseResult = JSON.parse(getEnderecoLog);' will probably be something like parseResult.data.bairroco address
– HudsonPH
But that always gets the last one. And if the goal is to always get the last one (which I doubt) then it’s easier to do
bairrocadastrado = parseResult[parseResult.length-1].bairroendereco
which avoided going through the whole list– Isac
to summarize all this in
.last()
@Isac– HudsonPH