0

Use-Case: I would like to go through the array of objects, checking each action until one of the actions equals a specific array [ 'sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage' ],.

I understand, however, I cannot use the === after reading this question.

Array:

[
    {
      Action: [
        'logs:CreateLogDelivery',
      ],
    },
    {
      Action: [ 'sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage' ],
      Effect: 'Allow',
      Resource: {
        'Fn::ImportValue': 'XXXXXX"
      }
    }

]

How can I do it so I can return true or false?

2
  • You can use === if comparing strings Commented Feb 16, 2023 at 15:24
  • Yes, just not arrays. Commented Feb 16, 2023 at 15:24

4 Answers 4

2

Check if some() filters has every() Action includes() in the array

const search  = [ 'sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage' ];
const filters = [
    { Action: ['logs:CreateLogDelivery'] },
    { Action: [ 'sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage' ], Effect: 'Allow', Resource: {'Fn::ImportValue': 'XXXXXX'} }
];

const searchIsInSomeFilter = filters.some(filter => {
    return search.every(searchValue => {
        return filter.Action.includes(searchValue);
    });
});

console.log(searchIsInSomeFilter);

// One-liner with short return:
// const searchIsInSomeFilter = filters.some(filter => search.every(searchValue => filter.Action.includes(searchValue)));

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

2 Comments

This answer is great! Could you clean up the ";;", make f and s more descriptive, and add some notes for other readers to easily understand? I'll mark it as the answer for this question.
Yea Ill apply those changes! - I hope this is better!
0

you can use the function Array.includes on an array to know if a given string is present or not.

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Comments

0

Deep equality checks the depths of objects so that every child (and child's child, etc.) is equal to the other. You can write your own, but many libraries exist to solve this problem. Here are some solutions:

Comments

0

I'd just make a function to compare two Arrays.

const array = [
    {
      Action: [
        'logs:CreateLogDelivery',
      ],
    },
    {
      Action: [ 'sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage' ],
      Effect: 'Allow',
      Resource: {
        'Fn::ImportValue': 'XXXXXX'
      }
    }
];

function checkArr(arr, actions) {
  for (const obj of arr) {
    if (isArrayEqual(obj.Action, actions)) {
      return true
    }
  }
  return false
}

function isArrayEqual(a, b) {
  for (const aa of a) {
    if (!b.includes(aa)) {
      return false
    }
  }
  for (const bb of b) {
    if (!a.includes(bb)) {
      return false
    }
  }
  return true
}

console.log(checkArr(array, ['sqs:GetQueueAttributes', 'sqs:GetQueueUrl', 'sqs:SendMessage']));

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.