0

I'd like to know IF a value is missing which one is missing.

Array1: You have these values at the moment

['cloud:user', 'cloud:admin']

Array2: You need to have these values in order to continue

['cloud:user', 'cloud:admin', 'organization:user']

Current method which returns true or false. But I'd like to know IF a value is missing which one is missing. For example: 'organization:user'. If nothing is missing return true.

let authorized = roles.every(role => currentResourcesResult.value.includes(role));
The M
  • 641
  • 1
  • 7
  • 33
  • Does this answer your question? [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – A l w a y s S u n n y Apr 10 '20 at 12:51

2 Answers2

1

Just use filter method and check whether some of elements of arr1 is not equal to an element of an arr2:

const missingValues = arr2.filter(f => !arr1.some(s => s ==f));

An example:

let arr1 = ['cloud:user', 'cloud:admin']
let arr2 = ['cloud:user', 'cloud:admin', 'organization:user'];
const missingValues = arr2.filter(f => !arr1.some(s => s ==f));
console.log(missingValues);
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

You can check it by using Array.prototye.filter().

const domain = ['cloud:user', 'cloud:admin', 'organization:user'];
const data = ['cloud:user', 'cloud:admin'];

const notInDomain = domain.filter(item => data.indexOf(item) === -1);

if (notInDomain.length > 0) {
  console.log('Some values are not in the domain set');
}

console.log(notInDomain);

Update

You can gain the same result by using includes instead of indexOf.

const notInDomain = domain.filter(item => !data.includes(item));
Community
  • 1
  • 1
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30