regex
regex

Reputation: 101

String in array javascript

I have a javascript array like this :

["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"]

I want to make like this:

["444c-0300-0b29-1817", "444c-0300-0b29-0715", "444c-0300-0b29-0720"]

I need a best practise.. Thanks for helping.

Upvotes: 0

Views: 62

Answers (2)

Redu
Redu

Reputation: 26161

One other way of doing this job;

var arr = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"],
    brr = [].concat(...arr.map(s => s.split(",")));
console.log(brr);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could use Array#reduce with Array#concat

var data = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"],
    single = data.reduce(function (r, a) {
        return r.concat(a.split(','));
    }, []);

console.log(single);

ES6

var data = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"],
    single = data.reduce((r, a) => r.concat(a.split(',')), []);

console.log(single);

Upvotes: 1

Related Questions