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.
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.