I have an object with the following formatting from an endpoint that I don't control:
{
"Day": "08-01-2021",
"Impressions": "34,058",
"Visits": "673",
"VisitRate": "6.19%",
"CostPerVisit": "$0.90",
"UsersReached": "10,865",
"Conversions": "88",
"OrderValue": "$5,932.18",
"ConversionRate": "13.08%",
"AverageOrderValue": "$67.41",
"ROAS": "9.76",
"CPA": "$6.91",
"Spend": "$607.96"
},
{
"Day": "08-02-2021",
"Impressions": "28,419",
"Visits": "807",
"VisitRate": "6.76%",
"CostPerVisit": "$0.74",
"UsersReached": "11,939",
"Conversions": "108",
"OrderValue": "$7,666.47",
"ConversionRate": "13.38%",
"AverageOrderValue": "$70.99",
"ROAS": "12.80",
"CPA": "$5.55",
"Spend": "$599.03"
},
{
"Day": "08-03-2021",
"Impressions": "34,278",
"Visits": "927",
"VisitRate": "8.03%",
"CostPerVisit": "$0.61",
"UsersReached": "11,539",
"Conversions": "160",
"OrderValue": "$11,838.38",
"ConversionRate": "17.26%",
"AverageOrderValue": "$73.99",
"ROAS": "20.94",
"CPA": "$3.53",
"Spend": "$565.35"
}
I need to use this data in a charting library that doesn't support numeric values with dollar signs, or commas (although percentages seem to fine).
I can loop through the object and create a new object if I know the keys, but I am absolutely lost on how to do this dynamically where the keys are not known (the keys are not known all the time).
EDIT:
I ended up doing the following. I'm using lodash since it's already exposed in my application:
_.each(json.data, function(entry, index) {
_.each(entry, function(v, k) {
json.data[index][k] = v.replace('$', '').replace(',', '');
})
});
Is this "correct"?
you can get a list of keys by using the Object.keys() function
let keys = Object.keys(objFromApi)
after you get a list of keys you could iterate through them and remove '$' sign etc.
// loop through keys
for(const key of keys){
// correct value
objFromApi[key] = objFromApi[key].replace('$', '')
}