遇到 TypeScript 多层次键映射对的问题

huangapple go评论111阅读模式
英文:

Facing issues with typescript multi hierarchy key map pair

问题

我要的是这段代码在我尝试像这样做时能够平稳运行:dependencyMetric.MyService.failure或dependencyMetric.MyService.time,目前得到了很多对象和其他东西,不知道如何将其变成一个层次结构映射。

英文:

How do I change this structure so that I can get the values of this object like a map
this is my const object

  1. const dependencyMetrics : {[dependancyName : string] : {[name : string] : MetricInfo }[]} = {
  2. MyService : [{
  3. failure : {
  4. metric: new Metric({
  5. namespace: 'namespace',
  6. metricName: 'ABC'
  7. }),
  8. metricProperties: {
  9. period: Duration.seconds(60),
  10. statistic: 'Average'
  11. }
  12. },
  13. time : {
  14. metric: new Metric({
  15. namespace: 'namespace',
  16. metricName: 'age'
  17. }),
  18. metricProperties: {
  19. period: Duration.seconds(60),
  20. statistic: 'Average'
  21. }
  22. }
  23. }]};
  24. //I am using this also as a type :
  25. type MetricInfo = {
  26. metric : Metric;
  27. metricProperties : MetricOptions;
  28. }

What I want is this code to behave smoothly when I try some thing like : dependencyMetric.MyService.failure
or dependencyMetric.MyService.time

currently getting a lot of Objects and stuff don't know how to make this a hierarchy map

答案1

得分: 0

你目前的结构将 MyService 视为对象数组的键。这就是为什么在尝试访问 failuretime 时会得到意外的结果。

从我的理解,你希望 MyService 成为一个包含 failuretime 指标的对象键,而不是一个数组。

尝试这样做:

  1. const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
  2. MyService : {
  3. failure : {
  4. metric: new Metric({
  5. namespace: 'namespace',
  6. metricName: 'ABC'
  7. }),
  8. metricProperties: {
  9. period: Duration.seconds(60),
  10. statistic: 'Average'
  11. }
  12. },
  13. time : {
  14. metric: new Metric({
  15. namespace: 'namespace',
  16. metricName: 'age'
  17. }),
  18. metricProperties: {
  19. period: Duration.seconds(60),
  20. statistic: 'Average'
  21. }
  22. }
  23. }
  24. };
  25. 你的 `MetricInfo` 类型将保持不变

编辑:

首先,要将更多的服务添加到你的 dependencyMetrics 对象中,你只需要在对象中包含它们:

  1. const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
  2. MyService1 : {
  3. // ...MyService1 的指标
  4. },
  5. MyService2 : {
  6. // ...MyService2 的指标
  7. },
  8. // 添加所需的服务
  9. };

然后,要动态访问特定服务的指标,你可以使用方括号表示法来获取与键关联的值:

  1. let serviceName = 'MyService1'; // 更改为你想要的服务名称
  2. let metricName = 'failure'; // 更改为你想要的指标
  3. let specificMetric = dependencyMetrics[serviceName][metricName];
英文:

Your current structure treats MyService as a key for an array of objects. Which is why your getting unexpected results when trying to access failure or time

From what i understood, you want MyService to be a key for an object containing failure and time metrics, not an array.

Try this:

  1. const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
  2. MyService : {
  3. failure : {
  4. metric: new Metric({
  5. namespace: 'namespace',
  6. metricName: 'ABC'
  7. }),
  8. metricProperties: {
  9. period: Duration.seconds(60),
  10. statistic: 'Average'
  11. }
  12. },
  13. time : {
  14. metric: new Metric({
  15. namespace: 'namespace',
  16. metricName: 'age'
  17. }),
  18. metricProperties: {
  19. period: Duration.seconds(60),
  20. statistic: 'Average'
  21. }
  22. }
  23. }
  24. };

Your MetricInfo type will stay the same

Edit:

First, to add more services to your dependencyMetrics object, you can just include them in the object:

  1. const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
  2. MyService1 : {
  3. // ...metrics for MyService1
  4. },
  5. MyService2 : {
  6. // ...metrics for MyService2
  7. },
  8. // add as many services as you need
  9. };

Then, to access the metrics of a specific service dynamically, you can use the bracket notation to get the value associated with a key:

  1. let serviceName = 'MyService1'; // change this to the service name you want
  2. let metricName = 'failure'; // change this to the metric you want
  3. let specificMetric = dependencyMetrics[serviceName][metricName];

huangapple
  • 本文由 发表于 2023年7月14日 01:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定