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

246
Visualizações
How to map through an object in JavaScript and extract nested data?

I'm trying to iterate through this data structure to generate a Graph. But I couldnt figure out how to map through my Objects.

{
 "dP008S002D007":[
   571.8619718309859,
   604.5888966971188
  ],
 "dP009S002D007":[
   1445.2859154929577,
  1503.5495432185523
 ]
}

dp00... will be my line. So I'll map over this object and generate a Line, the values inside the array will be my data. But I cant figure out how to map over this structure. This was a dictionary and I managed to reduce to this. Here is my code.

  const chartPoints = useMemo(() => {
    const history = iotData?.history?.[location.id].sensors || [];
    const chartPoints = {};
    const findPoints = Object.keys(history).map((key) => {
      chartPoints[key] = history[key].map((data) => data.avg);
    });
    return chartPoints;
  }, [iotData?.history, location.id]);

It'll be ideal if I could return:

[
 "dP008S002D007":[
   571.8619718309859,
   604.5888966971188
  ],
 "dP009S002D007":[
   1445.2859154929577,
  1503.5495432185523
 ]
]

Or map over these objects. Can Anybody help me with this? Thanks for all.

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

0

As some answered to your question, you can't make a variable like you wanted in your output. But, because you just want to iterate throught your input you can use the for...in

In a for...in you can iterate like this :

for(key in obj) {
//key is the name of the key like "dP009S002D007"
//obj[key] is the value like [1445.2859154929577,1503.5495432185523]
}

else you can also transform your whole object as array of array with this one :

let table = []
for(key in obj) {
    table.push([key, ...obj[key]])
}

it produces 1 array containing 2 arrays like this :

[ [ 'dP008S002D007', 571.8619718309859, 604.5888966971188 ],
[ 'dP009S002D007', 1445.2859154929577, 1503.5495432185523 ] ]

You will then be able to use .map on the table and do what you wanted.

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