1

How would I split this string and keep only the unique values using underscore? I do need the br after each value.

Input = 'John Doe<br> John Doe<br> Frank Watts<br> '

Expected Ouput = "John Doe<br> Frank Watts<br> '

What I've tried so far was to split the string using

str.split(' ') 

but this breaks the first and last name

seasick
  • 1,094
  • 2
  • 15
  • 29

3 Answers3

2

Using underscore:

var str = 'John Doe<br> John Doe<br> Frank Watts<br>',
    result = _.uniq(str.split(/\s*<br>\s*/)).join('<br> '); // "John Doe<br> Frank Watts<br> "
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

So if you want a unique list of names you have to slit it by the <br> and then add to a unique set. I.e:

  var array = str.split('<br> '); // get a list of all the names
  var unique=array.filter(function(itm,i,array){
      return i==array.indexOf(itm);
  });

if you wanted to keep the <br> then you could just join('<br>') after the split

Jay
  • 2,656
  • 1
  • 16
  • 24
0

From your question i have no idea what exactly you want, but if you want to split your string you can use this:

    var input = 'John Doe<br> John Doe<br> Frank Watts<br> '
    var arr = input.split(' ');
    console.log(arr[0]+' '+arr[1]+' '+arr[4]+' '+arr[5])
Dominik Vávra
  • 121
  • 1
  • 9