Skip to main content
Participant
October 24, 2024
Question

How to either remake my drop box or remove duplicates?

  • October 24, 2024
  • 9 replies
  • 856 views

Currently I have a set of six text fields that randomly generate a number. Lets call the A1, A2, A3, A4, A5, and A6.


I have a drop down menu where I want to populate with options based off the values of the text fields. 

 

Option A will always be present. Option B is only present if A1 or A4 is above 10. Option C is only present if A2 or A6 is above 10. Option D is only present if A3 is above 10. Option E is only available if A5 is above 10. 

 

The way I have it setup now will create doubles if A1 and A4 are both above 10 or Option C if A2/A6 are above the threshold.

 

var cDrop = ["-Select-", "Option A"];

var fields = ["A1", "A2", "A3", "A4", "A5", "A6"];

var drop = ["Option B", "Option C", "Option D", "Option B", "Option E", "Option C"];

for (var j = 0; j<fields.length; j++) {

if (Number.this.getField(fields[j].valueAsString) >=  10) {

cDrop.push(drop[j]);}}

 

this.getField("Dropdown Menu").setItems(cDrop);

This topic is closed to new replies. Start a new post to keep the conversation going.

9 replies

Nesa Nurani
Community Expert
Community Expert
October 24, 2024

Try like this:

var cDrop = ["-Select-", "Option A"];

var A1 = Number(this.getField("A1").valueAsString);
var A2 = Number(this.getField("A2").valueAsString);
var A3 = Number(this.getField("A3").valueAsString);
var A4 = Number(this.getField("A4").valueAsString);
var A5 = Number(this.getField("A5").valueAsString);
var A6 = Number(this.getField("A6").valueAsString);

if (A1 > 10 || A4 > 10) {
 cDrop.push("Option B");}

if (A2 > 10 || A6 > 10) {
 cDrop.push("Option C");}

if (A3 > 10) {
 cDrop.push("Option D");}

if (A5 > 10) {
 cDrop.push("Option E");}

this.getField("Dropdown Menu").setItems(cDrop);
Participant
October 25, 2024

Thank you all! It works!!

PDF Automation Station
Community Expert
Community Expert
October 24, 2024

Replace your last line with this script:

 

function removeDuplicates(arr) {
return arr.filter((item,
index) => arr.indexOf(item) === index);
}

this.getField("Dropdown Menu").setItems(removeDuplicates(cDrop));

Nesa Nurani
Community Expert
Community Expert
October 24, 2024

He will still have error because how he access field value,

I tried to simplify it for him in my script, but if he wants to use your addition he needs to change his script:
Updated script:

var cDrop = ["-Select-", "Option A"];

var fields = ["A1", "A2", "A3", "A4", "A5", "A6"];
var drop = ["Option B", "Option C", "Option D", "Option B", "Option E", "Option C"];

for (var j = 0; j < fields.length; j++) {
 var fieldValue = Number(this.getField(fields[j]).valueAsString);
 if (fieldValue >= 10) {
  cDrop.push(drop[j]);}}

//PDF Automation Station, addition to script
function removeDuplicates(arr) {
return arr.filter((item,
index) => arr.indexOf(item) === index);
}

this.getField("Dropdown Menu").setItems(removeDuplicates(cDrop));
PDF Automation Station
Community Expert
Community Expert
October 24, 2024

I posted to replace the last line, not the entire script. 🙂