0

I have the following JSON data. How can I loop through JSON data and find possible combination based on the input of names provided?

Dimensions array:

[
  {"value":"zabbixPy-API","name":"ApiName"}, 
  {"value":"qa","name":"Stage"}
]

Example: I'm providing the following uniqueMetricsArray which might exists in the Dimensions array

ApiId
ApiName
Stage    

Example of how variable key is constructed.

function test(name) {
  if (Dimensions.name[0] && Dimensions.name[1] && Dimensions.name[2]) {
    key = Dimensions.name[0].value + "_" + Dimensions.name[1].value + "_" + Dimensions.name[2].value;
  } else if (Dimensions.name[0] && Dimensions.name[1]) {
    key = Dimensions.name[0].value + "_" + Dimensions.name[1].value;
  } else if (Dimensions.name[0]) {
    key = Dimensions.name[0].value;
  } else {
    key = Metrics[].MetricName;
  }

  return key;
}

test(uniqueMetricsArray);

But in my case, the uniqueMetricsArray lenght might be 4 or 5. So I don't want to write all possible if conditions. Is there a way I can loop through based on the input name array length and find all possible combination it might exists and construct the variable key?

So Dimensions array can be:

 ApiName + Stage + ApiId
 ApiName + Stage
 ApiId + Stage
 ApiId + ApiName  
 Stage
 ApiId

Full Json Data:

const jsondata = {
  AlarmName: "Zabbix PY 5XX - By Stage QA",
  AlarmDescription: null,
  AWSAccountId: "123456",
  NewStateValue: "ALARM",
  NewStateReason:
    "Threshold Crossed: 1 out of the last 1 datapoints [1.0 (06/11/21 12:16:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
  StateChangeTime: "2021-11-06T12:17:43.811+0000",
  Region: "Asia Pacific (Mumbai)",
  AlarmArn: "arn:aws:cloudwatch:ap-south-1:123456:alarm:Zabbix PY 5XX - By Stage QA",
  OldStateValue: "INSUFFICIENT_DATA",
  Trigger: {
    MetricName: "5XXError",
    Namespace: "AWS/ApiGateway",
    StatisticType: "Statistic",
    Statistic: "MINIMUM",
    Unit: null,
    Dimensions: [
      { value: "zabbixPy-API", name: "ApiName" },
      { value: "qa", name: "Stage" },
    ],
    Period: 60,
    EvaluationPeriods: 1,
    ComparisonOperator: "GreaterThanOrEqualToThreshold",
    Threshold: 1,
    TreatMissingData: "- TreatMissingData: missing",
    EvaluateLowSampleCountPercentile: "",
  },
};

output of console.log(uniqueMetricsArray):

ApiId
ApiName
Stage    

This is how the actual if condition might look like. I just shortened it in the example above:

  if (sns.Trigger.Dimensions.find(dimension => dimension.name === name[0]) && sns.Trigger.Dimensions.find(dimension => dimension.name === name[1]) && sns.Trigger.Dimensions.find(dimension => dimension.name === name[2]) ) {
    var key = sns.Trigger.Dimensions.find(dimension => dimension.name === name[0]).value  + '_' + sns.Trigger.Dimensions.find(dimension => dimension.name === name[1]).value + '_' + sns.Trigger.Dimensions.find(dimension => dimension.name === name[2]).value
  }  
user630702
  • 2,529
  • 5
  • 35
  • 98
  • what will happen if one item in the uniqueMetricsArray doesn't exist on the Dimensions? – I am L Nov 22 '21 at 12:00
  • It always has at least one or none. I'm providing `uniqueMetricsArray ` to and want to check if the combination exists. If one of them doesn't exist that means check for other combination. Check `So Dimensions array can be` in the question. That's all the possible combination it can exists if array has 3 items. – user630702 Nov 22 '21 at 12:03
  • sorry I'm still confused, so if `uniqueMetricsArray ` = `['a', 'b','c']` and `Dimensions` = `[{value: 'test, name: 'a'},{value: 'test1', name:'b'}]`. Will the results be `a + b +c`, `a + b`, `b + c`, `a`, `b`, `c` ? or c wouldn't exist on the result? – I am L Nov 22 '21 at 12:06
  • results will be `b +c`. The `uniqueMetricsArray` is listing the possible values that might exists and doesn't mean all three exists. – user630702 Nov 22 '21 at 12:27
  • how is it `b + c` if `c` doesn't exist on `Dimensions`? – I am L Nov 23 '21 at 05:10

1 Answers1

0

Sure. This is based on this answer to generate all combinations, only adapted to work with arrays (assuming your strings don't have NUL characters).

function generate(array) {
  if (!array.includes("")) array = [...array, ""]; // need the empty string to generate all combinations
  return array
    .flatMap((v, i) => array.slice(i + 1).map((w) => `${v}\x00${w}`))
    .map((atom) => atom.split("\x00").filter(Boolean));
}

console.log(generate(["ApiId", "ApiName", "Stage"]));

outputs

[
  [ 'ApiId', 'ApiName' ],
  [ 'ApiId', 'Stage' ],
  [ 'ApiId' ],
  [ 'ApiName', 'Stage' ],
  [ 'ApiName' ],
  [ 'Stage' ]
]
AKX
  • 152,115
  • 15
  • 115
  • 172