0

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.

1
  • You can also use var arrayVal = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"]; arrayVal = arrayVal.join().split(","); Commented Aug 19, 2016 at 11:13

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.