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! 🙂
Solved! Go to Solution.
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
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
Interesting.. this sheds some more light on how to do certain things in Velocity. Merci!