3

Array input: [["test","test"],["test2","test"],["test2","test2"],["test","test2"]]

Array output: ["test test","test2 test","test2 test2","test test2"]

I'm able to obtain this output with:

output = input.join("|").replace(/,/g," ").toString().split("|")

However, I don't really like this workaround because:

  • It seems unnatural
  • If one of the arrays contains a comma itself, it will be also removed
  • If one of the arrays contains a pipe itself, the split will be not as expected

How can I get the output without those handicaps?

1
  • #FP: a generic util var join = glue => arr => Array.prototype.join.call(Object(arr), glue); and it's usage arr.map( join(" ") ); works on anything that looks like an Array. Commented May 24, 2016 at 19:43

1 Answer 1

10

Instead of joining the outer array, you can use map to join each inner array separately:

var arr = [["test","test"],["test2","test"],["test2","test2"],["test","test2"]];
var output = arr.map(subarr => subarr.join(' '));
Sign up to request clarification or add additional context in comments.

1 Comment

It works perfect, thx. Thanks also for the map method. It looks really powerfull.

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.