adobelivecycle

Select distinct element from a dataNode in Adobe Livecycle ES4


I have this original method

var firstLine = ""

    if( frmStock.txtPRSID.rawValue <> null or frmStock.txtPRSDesp.rawValue <> null )
then
    firstLine = Concat(frmStock.txtPRSID.formattedValue," ",frmStock.txtPRSDesp.formattedValue)
endif

var secondLine = ""

if( frmStock.frmSN.txtSN.rawValue <> null )
then
    var snData = ref(frmStock.frmSN.txtSN.dataNode.parent)
    var snSet = ref(snData.resolveNodes("$.MaterialSerialID[*]"))
    var temp = ""
    for i=0 upto snSet.length-1
    do
        var Content = ref(snSet.item(i).resolveNode("$"))
        if( Content <> null )
        then
            temp = Concat(temp,Content.value)
            if( i <> snSet.length - 1 )
            then
                temp = Concat(temp,"\u000a")
            endif
        endif
    endfor
    secondLine = temp
endif

if( secondLine == "" )
then
    secondLine = Concat(frmStock.txtType.formattedValue," ",frmStock.txtID.formattedValue)
endif

this.rawValue = Concat(firstLine,"\u000a",secondLine)

I'm trying to select only unique and distinct element from this part of the original method :

/*This returns stuff like P0001 , P3333, P0001 , P444, P3333 */

 var snData = ref(frmStock.frmSN.txtSN.dataNode.parent)
    var snSet = ref(snData.resolveNodes("$.MaterialSerialID[*]"))
    var temp = ""
    for i=0 upto snSet.length-1
    do
        var Content = ref(snSet.item(i).resolveNode("$"))
        if( Content <> null )
        then
            temp = Concat(temp,Content.value)
            if( i <> snSet.length - 1 )
            then
                temp = Concat(temp,"\u000a")
            endif
        endif
    endfor

So I thought about implementing a simple Solution is to use twp nested loops using this logic inspired from here

// Pick all elements one by one
        for (int i = 0; i < list.lengh; i++)
        {
            // Check 
            int j;
            for (j = 0; j < i; j++)
            if (arr[i] == arr[j])
                skip

            // If not add it to list 
            if (i == j)
            add to list 
        }

So I tried applying the logic to my method

var snData = ref(frmStock.frmSN.txtSN.dataNode.parent)
    var snSet = ref(snData.resolveNodes("$.MaterialSerialID[*]"))
    var temp = ""
    for i=0 upto snSet.length-1
    do
        for j=0 upto i-1 

    do
        var tempI= ref(snSet.item(i).resolveNode("$"))
         var tempJ= ref(snSet.item(j).resolveNode("$"))
         if(tempI <> tempJ) 
             then 
             break
         if(i==j) then 
        var Content = ref(snSet.item(i).resolveNode("$"))
        if( Content <> null )
        then
            temp = Concat(temp,Content.value)
            if( i <> snSet.length - 1 )
            then
                temp = Concat(temp,"\u000a")
            endif
        endif
        endif
        endif
            endfor
    endfor

I don't face any errors but the list returned is empty, can someone help me ? How to return only distinct element from frmStock ?


Solution

  • I end up finding how

    I added a function

       function uniq(a) {
            return a.sort().filter(function(item, pos, ary) {
                return !pos || item != ary[pos - 1];
            })
        }
    

    then applied it to the data source of the items

    var data = snData.resolveNodes("$.MaterialSerialID[*]");
    var uniqData =  uniq(data);