0

I have a JSON object that has an array as below

{
  "objects": [
    {
      "severity": "LOW",
      "tags": [
        {
          "key": "account",
          "values": [
            "account2"
          ]
        },
        {
          "key": "accountId",
          "values": [
            "2"
          ]
        }
      ],
      "name": "object1"
    },
    {
      "severity": "HIGH",
      "tags": [
        {
          "key": "account",
          "values": [
            "account2"
          ]
        },
        {
          "key": "accountId",
          "values": [
            "2"
          ]
        }
      ],
      "name": "object2"
    },
    {
      "severity": "MEDIUM",
      "tags": [
        {
          "key": "account",
          "values": [
            "account44"
          ]
        },
        {
          "key": "accountId",
          "values": [
            "44"
          ]
        }
      ],
      "name": "object2"
    },
    {
      "severity": "HIGH",
      "tags": [
        {
          "key": "account",
          "values": [
            "account42"
          ]
        },
        {
          "key": "accountId",
          "values": [
            "42"
          ]
        }
      ],
      "name": "object2"
    }
  ]
}

I want to be able to go through the array using javascript and if severity is HIGH and acccountID matches 2 for example to set a warning. Even though account 2 might have a severity of LOW as shown, I still want to make the if condition true.

So something like if (objects.some(a =>(a.severity ==="LOW" && ...... or whatever better option there is.

1

2 Answers 2

2

You can loop through your object and match your properties like this

const data = { "objects": [ { "severity": "LOW", "tags": [ { "key": "account", "values": [ "account2" ] }, { "key": "accountId", "values": [ "2" ] } ], "name": "object1" }, { "severity": "HIGH", "tags": [ { "key": "account", "values": [ "account2" ] }, { "key": "accountId", "values": [ "2" ] } ], "name": "object2" }, { "severity": "MEDIUM", "tags": [ { "key": "account", "values": [ "account44" ] }, { "key": "accountId", "values": [ "44" ] } ], "name": "object2" }, { "severity": "HIGH", "tags": [ { "key": "account", "values": [ "account42" ] }, { "key": "accountId", "values": [ "42" ] } ], "name": "object2" } ] };

// Loop
for(let i = 0; i < data.objects.length; i++) {
  // Set severity
  const severity = data.objects[i].severity;
  // Set ID
  let id; for(let ii = 0; ii < data.objects[i].tags.length; ii++) if(data.objects[i].tags[ii].key === 'accountId') id = data.objects[i].tags[ii].values[0];
  // Do logic here
  if(severity === 'LOW' && id === '2') console.log(data.objects[i]);
}

Sign up to request clarification or add additional context in comments.

1 Comment

@dontke check my updated code - now it's pretty straightforward to match what you need
0

If you're loop-avert:

if (objects.some(e => e.severity === severity || e.tags.some(t => t.key === 'accountId' && t.values.includes(accountId)))) {
...

For clarity, that closure is:

return e =>
    e.severity === severity ||
    e.tags.some(t =>
        t.key === 'accountId' &&
        t.values.includes(accountId));

e.g.

const j = { "objects": [ { "severity": "LOW", "tags": [ { "key": "account", "values": [ "account2" ] }, { "key": "accountId", "values": [ "2" ] } ], "name": "object1" }, { "severity": "HIGH", "tags": [ { "key": "account", "values": [ "account2" ] }, { "key": "accountId", "values": [ "2" ] } ], "name": "object2" }, { "severity": "MEDIUM", "tags": [ { "key": "account", "values": [ "account44" ] }, { "key": "accountId", "values": [ "44" ] } ], "name": "object2" }, { "severity": "HIGH", "tags": [ { "key": "account", "values": [ "account42" ] }, { "key": "accountId", "values": [ "42" ] } ], "name": "object2" } ] };
    
function condition(severity, accountId) {
    return e =>
        e.severity === severity ||
        e.tags.some(t =>
            t.key === 'accountId' &&
            t.values.includes(accountId));
}
    
if (j.objects.some(condition('LOW', '2'))) {
    console.log("warning: low || 2");
}

if (j.objects.some(condition('SUPER_HIGH', '4048'))) {
    console.log("warning: super high || 4048");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.