1

How can I access Array value using the key name in nodejs? I'm getting undefined when trying to access it following way. I want the value to be print when calling the key.

   var sns_DimensionsValue  = sns.Trigger.Dimensions['ApiName']
   console.log(sns_DimensionsValue)

Json Data:

{
    "AlarmName": "Zabbix PY 5XX - ByName",
    "AlarmDescription": null,
    "AWSAccountId": 123456789",
    "NewStateValue": "ALARM",
    "NewStateReason": "Threshold Crossed: 1 out of the last 1 datapoints [1.0 (11/11/21 13:51:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
    "StateChangeTime": "2021-11-11T13:52:09.318+0000",
    "Region": "Asia Pacific (Mumbai)",
    "AlarmArn": "arn:aws:cloudwatch:ap-south-1:123456789:alarm:Zabbix PY 5XX - ByName",
    "OldStateValue": "INSUFFICIENT_DATA",
    "Trigger": {
        "MetricName": "5XXError",
        "Namespace": "AWS/ApiGateway",
        "StatisticType": "Statistic",
        "Statistic": "MINIMUM",
        "Unit": null,
        "Dimensions": [
            {
                "value": "zabbixPy-API",
                "name": "ApiName"
            }
        ],
        "Period": 60,
        "EvaluationPeriods": 1,
        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
        "Threshold": 1,
        "TreatMissingData": "- TreatMissingData:                    missing",
        "EvaluateLowSampleCountPercentile": ""
    }
}
1
  • There's no JSON in your question. "Json Data" is an object. ApiName is not a key but the value of the name key. Your actual problem is: how to find an object in an array of objects for a given property value? Commented Nov 11, 2021 at 14:17

1 Answer 1

0
sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName').value
Sign up to request clarification or add additional context in comments.

3 Comments

Will that printout the value zabbixPy-API?
Why didn't you vote to close as dupe?
@John Read the documentation for Array.prototype.find() and you will find the answer to your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.