Expand my Community achievements bar.

Sort Array to Remove Null, 0, Empty Values

Avatar

Level 5

This dynamic document is 99% done. I have hit a wall on how to prevent null, 0 or empty values from appearing in the BCC list of this page. I have created "if" "else" statements but for some reason they don't filter this null result. Could use a fresh pair of eyes here on a Friday afternoon. Any suggestions on how to prevent the BCC list from populating with the worthless values? Thanks.

http://https://workspaces.acrobat.com/?d=sc6DK4bAOK1czwHlBS3E0g

Here is the code the Array.prototype came from a StackOver flow thread. I have yet to be able to get this method or another method to even work in Firefox Scratchpad let alone in Livecycle. The array prototype and another Stackoverflow function still allow null values to pass through the function designed to filter them out.

//fucntion to eliminate duplicates

function eliminateDuplicates(nameArray)

{

  var i,

       len=nameArray.length,

       out=[],

       obj={};

  for (i=0; i<len; i++) {

    obj[nameArray[i]]=0;

  }

  for (i in obj) {

    out.push(i);

  }

   

   return out;

 

};

function cleanArray(sortedNames){

  var celeryArray = new Array();

  for(var i = 0; i<sortedNames.length; i++){

      if (sortedNames[i])

      {

       celeryArray.push(sortedNames[i]);

      }

                                           }

  console.println("celeryArray = " + celeryArray);

  return celeryArray;

 

}

//Count rows

var rows = resolveNodes("form1.Page1.Document.OnethruTen.OneThruFour.Four[*]"); //count the rows

var nRows = rows.length-1; //assign row count to variable

// Create new array to hold sorted array data[][][]...

var nameArray = new Array ();

//Loop through the number of #4 subform instances obtaining values for each textfield

for ( i=0; i<= nRows; i++)

{

    //Fill array with values from #4 subform

    nameArray[(i*4)] = xfa.resolveNode("form1.Page1.Document.OnethruTen.OneThruFour.Four["+ i +"]").Owner.rawValue;

    nameArray[((i*4)+1)] = xfa.resolveNode("form1.Page1.Document.OnethruTen.OneThruFour.Four["+ i +"]").DF.rawValue;

    nameArray[((i*4)+2)] = xfa.resolveNode("form1.Page1.Document.OnethruTen.OneThruFour.Four["+ i +"]").Mgr.rawValue;

    nameArray[((i*4)+3)] = xfa.resolveNode("form1.Page1.Document.OnethruTen.OneThruFour.Four["+ i +"]").Exec.rawValue;

}

//console.println(nameArray);

//[][][][][][].........

sortedNames = new Array();

//[0 Thomas, Snyder T][1 Bollings, Temera A][2 Kiomer, Ioda L][3 Berry, Steve J] [5....

var sortedNames = eliminateDuplicates(nameArray);

console.println("sortedNames =" + sortedNames);

var sortedClean = cleanArray(sortedNames)

console.println("sortedClean = " + sortedClean);

//Number of values in the sortedNames array

var nsortedValues = sortedNames.length;

console.println("nsortedValues =" + nsortedValues);

//Number of Rows to add to BCC list

var nccRows = Math.ceil((nsortedValues/2));

console.println("nccRows = " + nccRows);

//Variable to add number of rows in addInstance script below

var nBCCaddRows = (nccRows-2);

console.println("nBCCaddRows = " + nBCCaddRows);

if ( nBCCaddRows > 0 )

{

    for(i=1; i <= nBCCaddRows; i++)

        {

            this.resolveNode('form1.Page1.ActionOwner.Table1._Name[1]').addInstance(1);

                if (xfa.host.version < 8) {

                                            xfa.form.recalculate(1);   

                                          }                                           

        }

}

//Loop through the values of the array and place them in the BCC list

for ( i=1; i <= nccRows; i++)

{                               

               if ( sortedNames[((i*2)-2)] !=0 )                                                           

               {

    form1.Page1.ActionOwner.Table1.resolveNode("Name["+i+"]").TextField1.rawValue = sortedNames[((i*2)-2)];  //always even i=1 sortedNames[0]  i=2 sortedNames[2]

               }

              

               else

               {

               form1.Page1.ActionOwner.Table1.resolveNode("Name["+i+"]").TextField1.rawValue = " ";

               }

              

               if ( sortedNames[((i*2)-1)] != 0)

               {

    form1.Page1.ActionOwner.Table1.resolveNode("Name["+i+"]").TextField2.rawValue = sortedNames[((i*2)-1)]; //i=1 sortedNames[1]  i=2 sortedNames[3]

               }

              

               else

               {

               form1.Page1.ActionOwner.Table1.resolveNode("Name["+i+"]").TextField2.rawValue = " ";

               }

}

3 Replies

Avatar

Level 10

Hi,

The Array object has a filter method, so try something like;

var names = ["Thomas, Snyder", null, "Bollings, Temera", ,"Kiomer, Ioda", "Berry, Steve J"];

cleanNames = names.filter(function (element) { return element !== null && element !== undefined; });

console.println(cleanNames.toSource());

Bruce

Avatar

Level 5

Thank you Bruce.

Here is my adapted script:

var sortedNames = names.filter(eliminateDuplicates(nameArray){return nameArray !== null && element !== undefined;});

function "eliminateDuplicates" is declared in the begining of this script and works fine e.g. var sortedNames= eliminateDuplicates(nameArray);

I am getting an error in the console and "Check Script Sytax".

SyntaxError: missing ) after argument list

112:XFA:form1[0]:MetricPage

These don't work either:

sortedNames = names.filter(eliminateDuplicates(nameArray)){return nameArray !== null && element !== undefined;};

sortedNames = (names.filter(eliminateDuplicates(nameArray))

{

return nameArray !== null && element !== undefined;

}

                           );

While the "Check Script Sytax" tells me what is wrong "SyntaxError: missing ) after argument list" I don't know how to interpret this information. Thank you for your time.

Avatar

Level 10

Hi,

You need to retain the "function" keyword and you don't need the word eliminateDuplicates at all.

In your first alternative the parenthesis goes around the whole thing,  you have two closing ones after the function arguments, so it wasn't so much missing as in the wrong place.

Bruce