agustin
agustin

Reputation: 1351

Javascript - Reduce array of arrays

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:

How can I get the output without those handicaps?

Upvotes: 3

Views: 772

Answers (1)

Oriol
Oriol

Reputation: 287980

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

Upvotes: 10

Related Questions