1
Like I do to compare two variables. I have a variable that brings the neighborhood id, called bairrocadastrado, it returns the neighborhood ID equal 1093. Soon I have a foreach that returns all the neighborhoods registered in the system, type bairro_id 995, 1093 etc. Only that the problem is as follows. It finds the ID. It makes the comparison, but always returns the neighborhoods that are not equal, ie the second IF, and are equal. What could be wrong?
function areasAtendimento()
{
// inicia as variaveis
var id_atendimento = "";
var id_bairro = "";
var nomebairro = "";
var nomecidade = "";
var nomeestado = "";
var valorfrete = "";
var parseResult = "";
var cidadecadastrada = "";
var bairrocadastrado = "";
var resultBairro = "";
// pegar os itens do session storage
var getEnderecoLogado = sessionStorage.getItem("dadosendereco");
$.ajax({
url: urlBase + "areaatendimento",
method: 'GET',
success: function (retorno)
{
parseResult = JSON.parse(getEnderecoLogado);
bairrocadastrado = parseResult[parseResult.length-1].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('.', ',');
// bairro igual ao que está cadastrado
if (id_bairro == bairrocadastrado)
{
$('.entregatotal').html("R$" + valorfrete);
$('.bt-finalizar-01 .finalizar-compra-carrinho').css('display', 'block');
}
// bairro diferete, não exibe o botão
if (id_bairro != bairrocadastrado)
{
$('.entregatotal').html("R$ 0,00");
$('.bt-finalizar-01 .finalizar-compra-carrinho').css('display', 'none');
$('.nao-atendido').css('display','block');
}
})
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
console.log('Erro');
}
});
}
Javascript operators need to be more specific for ifs, use exactly the same '==' and '!=='
– AnthraxisBR
What gives
console.log(typeof id_bairro, id_bairro, typeof bairrocadastrado, bairrocadastrado);
?– Sergio
Felipe, did you see my question ? if you don’t know use the console make Alert 1 to 1 and answer here what gives you.
– Sergio
Yes, the problem is that it returns all the registered neighborhood ids and at the time of comparing makes the two ifs. Compare what exists and then the ones that don’t exist saying it’s different. More details in the footer here of the discussion.
– Felipe Michael da Fonseca
Which of the variables is an array?
– Sergio
bairrocadastrado is a sessionstorage array that takes the id of the neighborhood registered by the client that is 10813. Logo id_neighborhood are all registered neighborhoods that come within ajax, ie within the foreach. In this variable also comes the id of the neighborhood 10813
– Felipe Michael da Fonseca
And you want to know if
id_bairro
exists inbairrocadastrado
, right? What is the structure ofbairrocadastrado
? Is an array of strings/numbers?– Sergio
registered neighborhood comes from this sessionStorage being as number: Takes the last of the array [{"idcliente":"ecca2c6b-cd15-443b-b4db-5a15d39196c5","cliente_endereco_id":"69b8626d-c8d0-44a0-933a-e38e1a2737cd","cependereco":"89400000","logradouroendereco":"Rua Jaime Correia Pereira","numeroendereco":"90","complementoendereco":"Casa","referencia":"Centro Comunitário","estadoendereco":"Santa Catarina","cidadeendereco":"Porto União","bairroendereco":10813}]
– Felipe Michael da Fonseca
Ok, and in that array the value you want to compare is
bairroendereco
? is to check whetherid_bairro
is equal to"bairroendereco":10813
?– Sergio
yes that’s right. Only I got into the loop and it does not come out of it. It takes all who are and those who do not and compares.kkk
– Felipe Michael da Fonseca
Felipe, usa
@
before the name for me to receive a notification, otherwise it will be hours before you see your comment :) Okay, then one last question: this arraybairrocadastrado
may have more than one object inside? or always comes with 1 object inside the array?– Sergio
@Sergio, only 1 object the neighborhood id
– Felipe Michael da Fonseca
@Felipemichaeldafonseca ok, it would be case then you have
bairrocadastrado = parseResult[parseResult.length-1].bairroendereco[0].bairroendereco
? It’s quite long but it seems to me the minimum change to do in the code.– Sergio
yes @Sergio, but that would solve the IF problem?
– Felipe Michael da Fonseca
Yes, if is right. The problem is the variable
bairrocadastrado
not getting what you expected. Ifbairrocadastrado
is right theif
will work. You could haveif (id_bairro == bairrocadastrado)
and belowelse
in time ofif (id_bairro != bairrocadastrado)
. It was a little better for reading the code, but it’s not wrong and the problem is not the!=
==
in this case.– Sergio
sorry @Sergio, but returns the following error: Cannot read Property 'bairroendereco' of Undefined
– Felipe Michael da Fonseca
Okay, going back a step. Put here the value of
console.log(typeof getEnderecoLogado, getEnderecoLogado);
withoutJSON.parse
, right aftervar getEnderecoLogado =
.– Sergio
actually @Sergio if it works only I wanted it to show if the shipping of the bairroendereco is equal to the foreach id. But then he returns all those who are different too, because he is different understood? I believe the error is there, we just do not know how to make it return the correct values and to stop it in the first if it is equal and do not execute the second.
– Felipe Michael da Fonseca
@Sergio he returns: string [{"idcliente":"ecca2c6b-cd15-443b-b4db-5a15d39196c5","cliente_endereco_id":"69b8626d-c8d0-44a0-933a-e38e1a2737cd","cependereco":"89400000","logradouroendereco":"Rua Jaime Correia Pereira","numeroendereco":"90","complementoendereco":"Casa","referencia":"Centro Comunitário","estadoendereco":"Santa Catarina","cidadeendereco":"Porto União","bairroendereco":10813}]
– Felipe Michael da Fonseca
As for what comes from
getEnderecoLogado
the correct way isbairrocadastrado = parseResult[0].bairroendereco;
, since it is only 1. In relation to "it returns all that are different too" and "we do not know how to make it return the correct values" I still don’t understand what you mean. Refer toretorno
ajax or sessionStorage?– Sergio