I have an array containing JSON objects. Some of the properties' values in these objects need to be converted from strings to numbers. The goal is to create a new list with the modified JSON objects that have the number values. I attempted to use parseFloat for this task, but it was not successful. code
var json = [{
"id" : "0",
"msg" : "hi",
"cost" : "250.24",
"discount": "10"
},
{
"id" : "1",
"msg" : "there",
"cost" : "45.00",
"discount": "0"
}];
var json1 = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
for (const prop in json[key]) {
const parsed = parseFloat(json[key][prop]);
res[key][prop] = isNaN(parsed) ? json[key][prop] : parsed;
// need to push array of modified json object value converted to number from string into json1
}
}
}