Check for unique match possible via indexOf()?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:23 AM
Hi,
I'm using a indexOf Function, but need to know if there is exactly only one match?
Something like
if (variable.indexOf(key) == 1) {
was what came into my mind ... but it does not work ;-(
Could you please help out?
Thank you
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 09:46 AM
I'm checking for one sys_id.
If user is in one relevant group - it works
if in none it works (> else)
if in more than one - it works (>else)
if in 1 & one with 2 types (only one matches condition) it goes into the == 1 ... instead of the else path

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 09:57 AM
Can you cite some examples? I'm not following the logic for some reason. I thought your requirement was like this:
Group A: type 1, 2, 3
Group B: type 4
Group C: type 2
User X is in group A and B
User Y is in group B
User Z is in group C and A
If you ask the script I wrote "Is User X in exactly 1 group of type 2?" the answer is the sys ID of group A.
If you ask the script "Is User Y in exactly 1 group of type 2?" the answer is null/empty (because it only belongs to group B, which is only type 4.
If you ask the script "Is User Z in exactly 1 group of type 2?" the answer is null/empty (because it belongs to two)
Keep in mind, the code above is untested so I'm going on the INTENT of the code to write the above statements.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 10:10 AM
The question would be how many groups with a type 2 is the user assigned to. The answer should be 2. So not matching ==1
sorry that I was not more specific from the beginning.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 10:14 AM
And if the answer is 2, do you want an array of both sys_ids? what should the function return? What exactly is the output you expect? a "2"?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 10:19 AM
No. 2 is Else condition.
i only want the group if there is only one match.
I auto assign group with this only if only one match for relevant group is found