Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
How to change JSON String data to JSON Number data

Good day everyone, I'm trying to obtain the numerical value of 'review' what happens is that in the JSON it returns it as a String and therefore I cann't perform a sum that I did, resulting in a meaningless value

The objective is that, when someone makes a comment about an X product, the data is saved in Firebase in a JSON, the problem is that 'review' is saved as a String and not saved as a number

Is there a way to take this single piece of data and convert it to number?

This is the JSON response

"[{\"review\":\"4\",\"comment\":\"Good product \",\"name\":\"SnowFall\",\"image\":\"https://lh3.googleusercontent.com\",\"method\":\"google\"}]"

This is my TypeScript

callback(i, totalReviews) {

    if (!this.render) {

      this.render = true;

      let globalRating = 0;
      let globalReviews = 0;

      setTimeout(function () {

        totalReviews.forEach((review, index) => {

          globalRating += review.length;

          for (const i in review) {

            globalReviews += review[i].review;

            console.log("globalReviews", globalReviews);
          }
        })

        console.log("Raiting", globalRating);
        console.log("Reviews", globalReviews);

        let averageReviews = Math.round(globalReviews / globalRating);
        let precentage = Math.round(globalReviews * 100 / (globalRating * 5));

        $(".globalRating").html(globalRating);
        $(".percentage").html(precentage);

        let averageRating = DinamicReviews.fnc(averageReviews);

        $(".br-theme-fontawesome-stars").html(`

        <select class="ps-rating reviewsOption" data-read-only="true"></select>

        `)

        for (let i = 0; i < averageRating.length; i++) {

          $(".reviewsOption").append(`

          <option value="${averageRating[i]}">${i + 1}</option>

          `)

        }

        Rating.fnc();

      }, i * 10)
    }
  }

This is how it is saved in FireBase FireBase RealTime Database

This is how it looks on my FrontEnd FrontEnd

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is a line where you assign the .review value to the globalReviews variable as this is using += this shall be concatenating the string and that is the problem you are facing.

If you change that same line to: globalReviews += +review[i].review;

This will fix the error.

The reason why adding a + sign in front of a variable works is because this sign is known as the Unary Plus Operator which converting the value to a number or in case the store value is not a number it will convert following the Unary Plus Operator documentation

To make this code safe and thus not add a possible NaN you can failsafe to 0 by using the following syntax globalReviews += +review[i].review || 0;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda