How to check equal values of a Foreach

Asked

Viewed 269 times

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');
    }
});

inserir a descrição da imagem aqui

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 of retorno ajax.

  • 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

  • Because the parseResult.forEach(function (item) { bairrocadastrado = item.bairroendereco; } That one foreach will just take the ultimo item.bairroendereco. I couldn’t figure out what this one was about for

  • 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?

  • Voce does not need this foreach, Voce can directly pick up 'parseResult = JSON.parse(getEnderecoLog);' will probably be something like parseResult.data.bairroco address

  • 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

  • to summarize all this in .last() @Isac

Show 2 more comments

1 answer

1

With ES6 you can use "includes" as an example below:

var arr = [ 1, 2, 3, 5, 6, 7, 8 ]
arr.includes(1) //resultado true
arr.includes(4) //resultado false

If ES6 is not used, you can see on the Mozilla website the polyfill of "includes".

Mozilla Polyfill includes

  • This would not be a good solution, first pq is prototype and second pq does not have support for all browsers.

  • Yes and I know that, so I sent a link with polyfills to this resource, and if I use this in an npm project it’s even easier. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.