- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 11:54 AM - edited 11-15-2022 12:08 PM
Hello!
I have a very simple requirement, but I am not quite familiar with array concept - for loops, so please help me
I am storing certain values in arrays
property1 = [val1,val2,val3];
property2 = [val4,val5,val6];
property3 = [val7,val8,val9];
In my table, there is a reference field say 'reffield 'and a choice field 'choifield'.
the reference field's display values are generally val1,val2,etc (which are being stored as properties)
so,
If my reffield is one of val1/val2/val3 --> my choifield should be set to choice1
if reffield is one of val4/val5/val6 --> choifield should be set to choice2
if reffield is one of val7/val8/val9 --> choifield should be choice3.
simple if else script is working but there are many values like val1,val2..... It is hard to put all these in if condition.
How to do this using for loops or any other method?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 11:38 PM
Hi, normally this type of scenario would be driven by something like a related records table, dependent value, ref qualifier or similar. So potentially you take refField, lookup the custom table for a match, and return the mapped 'choice' value.
Another simple method would be to concatenate you values as a comma separated string (or keep in an array if you like) but use string indexOf() method to see if your refField exists in the string, if true return choiceX and this reduces your code to simple to follow if elseif conditions.
I see no value in searching your arrays using a loop as each array needs to be checked separately and all you are doing is a true\false check which is effectively just a variation on indexOf() and delivers no array specific functionality.
If you use indexOf() you can also maintain the reference 'strings' as system properties, which means any updates are configuration change not code and so may follow a less invasive change management cycle.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 02:48 PM
Hi, I believe that the solution that would be easiest for you to manage\understand would be to use 'Switch', this will also make your intentions clear for developers who follow you.
example with string or int values as the solution will depend on the 'value' used in your choices.
var refField = 'val7';
var myResult = '';
var myInt = 0;
switch (refField)
{
case 'val1':
case 'val2':
case 'val3':
myResult = 'choice1';
myInt = 1;
break;
case 'val4':
case 'val5':
case 'val6':
myResult = 'choice2';
myInt = 2;
break;
case 'val7':
case 'val8':
case 'val9':
myResult = 'choice3';
myInt = 3;
break;
default:
myResult = 'chocie4';
myInt = 4;
}
gs.info('myResult ' + myResult);
gs.info('myInt ' + myInt);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 08:34 PM
Hi @Tony Chatfield1 ,
Thanks for replying but the problem here is each set has around 25 vals and there are around 12 such sets. Here I just put three sets(property1,2,3) and each set has only 3 values just as an example. so it would be difficult for me to include 25 case statements in each of these sets. May I ask if there is any other way of doing it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 11:38 PM
Hi, normally this type of scenario would be driven by something like a related records table, dependent value, ref qualifier or similar. So potentially you take refField, lookup the custom table for a match, and return the mapped 'choice' value.
Another simple method would be to concatenate you values as a comma separated string (or keep in an array if you like) but use string indexOf() method to see if your refField exists in the string, if true return choiceX and this reduces your code to simple to follow if elseif conditions.
I see no value in searching your arrays using a loop as each array needs to be checked separately and all you are doing is a true\false check which is effectively just a variation on indexOf() and delivers no array specific functionality.
If you use indexOf() you can also maintain the reference 'strings' as system properties, which means any updates are configuration change not code and so may follow a less invasive change management cycle.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2022 12:08 AM
Thanks @Tony Chatfield1 , going with the indexOf method