Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!