SOLVED

Re: Velocity: Merge array lists and make values unique

Go to solution
lukea
Level 3

Velocity: Merge array lists and make values unique

Hi community

Given for instance:

 

#set( $foo = ["a", "b"] )
#set( $bar = ["b", "c"] )

How to a.) merge foo and bar into a new variable, which has b.) unique values:

["a", "b", "c"]

?

Thanks in advance! 🙂

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity: Merge array lists and make values unique

There’s no shortcut that also follows programming best practices.

 

The right way is to you create a new list $baz and then loop over each of the other lists, checking if $baz.contains each value and only add-ing it if it’s not already there.

 

The not-right but easier to write way (because of the wasted erase/write cycles, not that you’d ever notice in Marketo), assuming the contributing lists themselves don’t have duplicates, is:

#set( $baz = [] )
#foreach( $list in [$foo,$bar] )
#set( $void = $baz.removeAll($list) )
#set( $void = $baz.addAll($list) )
#end

 

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity: Merge array lists and make values unique

There’s no shortcut that also follows programming best practices.

 

The right way is to you create a new list $baz and then loop over each of the other lists, checking if $baz.contains each value and only add-ing it if it’s not already there.

 

The not-right but easier to write way (because of the wasted erase/write cycles, not that you’d ever notice in Marketo), assuming the contributing lists themselves don’t have duplicates, is:

#set( $baz = [] )
#foreach( $list in [$foo,$bar] )
#set( $void = $baz.removeAll($list) )
#set( $void = $baz.addAll($list) )
#end

 

lukea
Level 3

Re: Velocity: Merge array lists and make values unique

Interesting.. this sheds some more light on how to do certain things in Velocity. Merci!