Search code examples
javascriptarrow-functions

Transform string intro array using Arrow Function


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

var str = '[email protected];[email protected],[email protected]';

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

//desired result: {VALUE: '[email protected]'},
//                {VALUE: '[email protected]},
//                {VALUE: '[email protected]'}

Solution

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

    const
        str = '[email protected];[email protected],[email protected]',
        result = str
            .split(/[,;]/)
            .map(VALUE => ({ VALUE }));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }