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'}
TulioVargas
  • 137
  • 1
  • 5

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
  • 376,160
  • 25
  • 347
  • 392
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
  • 191,379
  • 34
  • 261
  • 317