This code intends to extract the value associated with the...
This code intends to extract the value associated with the key metric.label.store
from a deeply nested JSON object (context.panel.data.series
) and add it to a Set
to ensure unique values are collected. However, there is a typo in the key name when accessing the property.
The JSON property is named "metric.label.store"
(singular metric
), while the code tries to access "metrics.label.store"
(note the incorrect "metrics"
).
Here is the corrected code:
const storeSet = new Set();
context.panel.data.series.forEach((seriesObj) => {
// Correcting the typo in the key
storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]);
console.log(seriesObj.meta.custom.labels["metric.label.store"]);
});
console.log(storeSet);
Explanation of What This Code Does:
-
Initialize a Set: Creates an empty
Set
calledstoreSet
, which ensures that only unique values will be stored. -
Iterate over an Array: Loops through each object in the
series
array (accessible throughcontext.panel.data.series
). -
Access Nested Property:
- Goes through the chain of nested properties:
meta.custom.labels["metric.label.store"]
. - Adds the value of
metric.label.store
to thestoreSet
.
- Goes through the chain of nested properties:
-
Log the Values: Logs the value of
metric.label.store
for each object in the console. -
Log the Set: After all iterations, logs the
storeSet
, which will contain all uniquemetric.label.store
values.
Example Output:
If context.panel.data.series
is as provided, where metric.label.store
is "3727"
, the output will be:
3727
Set { "3727" }
This code ensures only unique values are collected. If there are multiple objects in context.panel.data.series
with different metric.label.store
values, the Set
will hold all unique values.