1

I was wondering if is possible to transform a string into an array using Arrow Function:

var str = 'Bob@example.com;Mark@example.com,robert@email.com';

var result = str.split(';').map(e => e.split(','))

//desired result: {VALUE: 'Bob@example.com'},
//                {VALUE: 'Mark@example.com},
//                {VALUE: 'robert@email.com'}

2 Answers2

2

You could split with a character class of comma and semicolon and map objects.

const
    str = 'Bob@example.com;Mark@example.com,robert@email.com',
    result = str
        .split(/[,;]/)
        .map(VALUE => ({ VALUE }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
1

You could use a regular expression to handle this

var str = 'Bob@example.com;Mark@example.com,robert@email.com';

var result = str.split(/[;,]/)

console.log(result);
Gabriele Petrioli
  • 183,160
  • 33
  • 252
  • 304