Tips for converting JSON String data to JSON Number data

Hello everyone, I am facing an issue with converting the 'review' value from String to a numerical format in JSON. This is causing problems when trying to perform calculations, leading to incorrect results.

The scenario involves saving user comments about a product in Firebase as JSON data. However, the 'review' field is stored as a String instead of a number. Is there a way to easily convert this data to a numerical format?

Below is the sample JSON response:

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

Here is a snippet of my TypeScript code:

---Code snippet removed for brevity---

This is how the data is structured in FireBase FireBase RealTime Database

And here is how it appears on the FrontEnd: FrontEnd

Answer №1

When assigning the value of .review to the globalReviews variable with the operator +=, it concatenates the strings, leading to the issue you are experiencing.

To resolve this problem, you can modify that line to:

globalReviews += +review[i].review;

By adding a + sign in front of a variable, you are utilizing the Unary Plus Operator, which converts the value to a number or handles non-numeric values according to the Unary Plus Operator documentation.

To prevent any potential addition of NaN, you can safeguard against it by defaulting to 0 using this syntax:

globalReviews += +review[i].review || 0;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I asynchronously parse JSON data from a URL on a Windows phone and display it in a

As an Android developer exploring the world of Windows Phone for the first time, I came across this resource on how to handle list boxes in Windows Phone 7/8. However, my challenge now is parsing JSON from a URL instead of XML as shown in the example. Whil ...

The Typescript module in question does not contain any exported components or functions related to

I've encountered an unusual issue while working on a React, Redux TypeScript application. It seems that after making some changes, one of the modules has stopped exporting its members. Here is the folder structure: src |---- components |---- contain ...

Display Descriptions Upon Hovering Over Images

I am attempting to utilize JavaScript to display the caption of an image only when it is being hovered over, and to have a default caption shown when no image is being hovered. <ul class="logos"> <li class="image-1"></li> <li ...

Showing Predefined Date on an HTML Form in an iOS Application

Is there a method to dynamically show the current date within the button that triggers the date picker in an HTML form on an iOS device? This is what currently appears: This is what I want it to automatically display: Below is the code I have implemente ...

Encountered a problem while trying to deserialize JSON using DataContractJsonSerializer

I'm facing a challenging dilemma. Currently, I am attempting to deserialize the JSON string retrieved from an ASP.NET Web Service: "{\"d\":{\"__type\":\"KPCServer.LogonResult\",\"User\":{\"UserId\":&b ...

Despite the presence of a producer and topic, sending Kafka messages is proving to be a challenge

Currently, I am using TypeScript and the KafkaJS library on my local machine with a single Kafka broker. After successfully connecting a producer, confirming the creation of my topic, and creating messages like so: const changeMessage = { key: id, ...

What is the best way to utilize JavaScript variables that are declared on index.html/index.jsp in Vue.js?

Just starting out with Vue.js and I recently finished developing a Vue.js app using the terminal. I then deployed it on a Java web application and noticed that everything works fine when running it as is. However, I now need to pass a csrftoken to my Vu ...

Problem with Typescript: The type '{ x;y }' is required to have a '[Symbol.iterator]()' method

Just starting out with Typescript and tackling the task of converting a React project from JavaScript to TypeScript. I've been diving into various posts for guidance, but I feel like I'm going in circles. Any assistance would be greatly appreci ...

Retrieve all elements associated with a specific key within an array

I need to gather all the "file_name" values into a single variable in order to store it as a string separated by commas in my database. array (size=13) 0 => array (size=14) 'file_name' => string '__0000132.jpg' ( ...

What is the method for comparing array elements to a regular expression in this particular scenario?

My array contains strings with a specific format: obj-meta_version-11_info obj-meta_version-12_info obj-meta_version-13_info I need to loop through this array to identify objects that adhere to this naming convention. I think the regex pattern should be ...

Creating a DynamoDB table and adding an item using CDK in Typescript

Can anyone guide me on how to add items to a Dynamodb Table using CDK and Typescript? I have figured out the partition/sort keys creation, but I am struggling to find a straightforward solution for adding items or attributes to those items. Additionally, ...

Automating the process of running npm start on page load: A guide

Recently, I've been delving into learning npm in order to incorporate it into a website. I'm curious about how exactly it is used within a website - do you typically need to execute the command "npm start"? How does this integration work for a li ...

The malfunctioning of my Jquery datepicker

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <script src="jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).r ...

Ways to reuse test cases across different test suites in Protractor

There are some shared test cases that can be utilized across different test suites. For example, suppose suite x and suite y both require the same set of test cases to function properly. To address this, a separate .js file containing the shared code has ...

What mistakes am I making in this PHP code as I try to work with the select option?

Currently, I am developing a form that involves selecting values. If the user chooses 'yes', I want to display a text box section. However, the code below is not functioning as expected. <select id="gap" name="gap" onclick="gap_textbox();"> ...

Converting JSON data into a jQuery-powered spreadsheet

Recently, I completed a module on data visualization, where I learned how to transform Google Spreadsheets into JSON using jQuery. In my spreadsheet, there are two simple columns: date and status (collected data about myself for practice in visualizing it ...

What is happening with the arrangement of $scope in Angular 1.x?

In my controller, I am loading content using ajax and want a spinner to appear while it's loading. The code snippet is as follows: <i class="fa fa-2x fa-spin fa-spinner" ng-show="isLoadingContent"></i> Accompanied by the following JavaSc ...

The results of the jQuery selector appear differently in the browser console compared to PhantomJS

What could be causing the discrepancy in results when using the same jQuery selector? Visit this website for reference: See below code involving node.js and phantomjs-node(bridge): phantom.create(function(ph){ ph.createPage(function(page){ p ...

How can I streamline a kendo UI MVC project by eliminating unnecessary components?

After switching my MVC 5 project to utilize Kendo UI, I've noticed a significant increase in the number of files being used. Since there is no need for supporting other cultures at the moment, can I confidently delete the files within the messages an ...

When using JsonConvert.DeserializeObject, it may struggle to locate integer members within the JSON object

I'm facing a strange issue where all my classes deserialize properly except for one particular class that contains three int values. When I use the trace writer setting, Json.Net reports that it cannot find the members for this specific class, and I a ...