Velocity: Merge array lists and make values unique | Community
Skip to main content
Level 3
October 11, 2022
Solved

Velocity: Merge array lists and make values unique

  • October 11, 2022
  • 1 reply
  • 3449 views

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! 🙂

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

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

 

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
October 12, 2022

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

 

lukeaAuthor
Level 3
October 12, 2022

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