Add values to a deserialized JSON object attribute in Javascript

1

My problem is this: I am retrieving a Json object from localStorage via Javascript, and wanted to add a value to one of these attributes, and then save the evolta object to local storage. However, when doing the operation: object.attribute = object.attribute + aValueNumeric; what happens is that the values are concatenated in the attribute. That is, instead of 1 + 1 result in two, the result is saved in Json as "11".

In this example, the client's current balance was 122.00 and I tried to add 8772.99.

The two leading zeros are because I have not been able to format the right pattern yet.

Here is my code:

function processaConfirmDep(){


        var valor = document.getElementById('valorDeposito');
        var favorecido = document.getElementById('nomeFav');



            var objDep;


            var cliente = localStorage.getItem(favorecido.value);
            var clienteConta = JSON.parse(cliente);
            var agenciaFav = clienteConta.agencia;
            var contaFav = clienteConta.conta;

            objDep = {
            id : "12",
            agenciaFav : agenciaFav.value,
            contaFav : contaFav.value,
            valor : valor.value
            }
            clienteConta.saldo = clienteConta.saldo+valor.value;

            var clienteDeposito = createMyObject("DepositoLucas", [clienteConta, objDep]);

            localStorage["DepositoLucas"] = JSON.stringify(clienteDeposito);
            localStorage[clienteConta.nome] = JSON.stringify(clienteConta);

        }

Result in the local Storage. (See the "Lucas" key object, "balance" attribute):

{"DepositoLucas":[{"nome":"Lucas","agencia":"33333","conta":"4123","saldo":"0122,008772,99"},{"id":"12","valor":"8772,99"}]}



Lucas {"nome":"Lucas","agencia":"33333","conta":"4123","saldo":"0122,008772,99"}
    
asked by anonymous 29.04.2018 / 15:29

1 answer

1

What is happening is that the value in the jason object is as string, so when trying to execute the sum, it is actually concatenating. In order to be able to do the mathematical calculation, there must be a conversion using parseFloat() , it follows below its code Js , corrected:

function processaConfirmDep(){

        var valor = document.getElementById('valorDeposito');
        var favorecido = document.getElementById('nomeFav');

        var objDep;

        var cliente = localStorage.getItem(favorecido.value);
        var clienteConta = JSON.parse(cliente);
        var agenciaFav = clienteConta.agencia;
        var contaFav = clienteConta.conta;

        objDep = {
        id : "12",
        agenciaFav : agenciaFav.value,
        contaFav : contaFav.value,
        valor : valor.value
        }

        var valorDepConvertido = parseFloat(valor.value);
        clienteConta.saldo = parseFloat(clienteConta.saldo)+valorDepConvertido;

        var clienteDeposito = createMyObject("DepositoLucas", [clienteConta, objDep]);

        localStorage["DepositoLucas"] = JSON.stringify(clienteDeposito);
        localStorage[clienteConta.nome] = JSON.stringify(clienteConta);

    }
    
29.04.2018 / 16:07