Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
Loop through JSON and reformat numbers/currency/percentages

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"?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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('$', '')
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda