After receiving JSON data from the server, I am required to convert it into a different format as shown below:
{"1":"82700","2":"26480","3":"31530","4":"22820","5":"15550","6":"205790"}
Although I have attempted to achieve this with the provided code snippet, it is not yielding the desired results. The current code iteration appears below:
var cities = "{";
for (var key in data.visits) {
var val = data.visits[key];
var obj = {};
obj[val.City] = '' + val.Count;
var code = '' + val.City;
var count = '' + val.Count;
cities += code + ':' + count + ',';
}
cities += "}";
The objective is to ensure that the integers are represented as strings and eliminate the trailing comma at the end of the collection. How can I rectify this code in order to accomplish the desired outcome?