简体   繁体   English

Javascript - 减少数组数组

[英]Javascript - Reduce array of arrays

Array input : [["test","test"],["test2","test"],["test2","test2"],["test","test2"]] 数组输入[["test","test"],["test2","test"],["test2","test2"],["test","test2"]]

Array output : ["test test","test2 test","test2 test2","test test2"] 数组输出["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? 如何在没有这些障碍的情况下获得输出?

Instead of joining the outer array, you can use map to join each inner array separately: 您可以使用map分别join每个内部数组,而不是连接外部数组:

var arr = [["test","test"],["test2","test"],["test2","test2"],["test","test2"]];
var output = arr.map(subarr => subarr.join(' '));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM