dontke
dontke

Reputation: 109

How to check if an array contains two values with Javascript

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.

Upvotes: 0

Views: 381

Answers (2)

root
root

Reputation: 6048

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");
}

Upvotes: 0

tarkh
tarkh

Reputation: 2549

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]);
}

Upvotes: 2

Related Questions