How to skip duplicate values in the apache velocity js

77 Views Asked by At

I want to skip the duplicate values so using below code but it seems not working.

    #set($uniqueEntities=[])
#foreach($e in $entities)
#if($foreach.index <=12)
#if($uniqueEntities != ($e.offerId))
#set($uniqueEntities=$e.offerId)
#end
$uniqueEntities,
#set($foreach.index = $foreach.index + 1)
#end
#end

Response : OFF-56938,OFF-44046,OFF-27626,OFF-60503,OFF-49318,OFF-52824,1355738,OFF-13099,OFF-27626,OFF-11757,1355717,OFF-27305,OFF-42752

I do not want OFF-27626 to reappear. How could we fix this please?

1

There are 1 best solutions below

3
Matthias On

Without actually knowing velocity.js, looking at this answer I think this might work for you

#set($uniqueEntities = [])
#foreach($e in $entities) 
    #if( ! $uniqueEntities.contains( $e.offerId )  )
        #if( $uniqueEntities.add($e.offerId) ) #end 
        ##note the if above is to trap a "true" return - may not be required
        $e.offerId
    #end
#end