简体   繁体   English

除了Array.prototype.join()以外,还有没有更短的方法将数组连接到一个字符串?

[英]Is there a shorter way to join an array to one string besides Array.prototype.join()?

I want to join an array to one string. 我想将一个数组连接到一个字符串。 Normally I would write: 通常我会写:

x=a=>a.join('')

x([1,2,3,4,5]);         // '12345'
x(['a','b','c','d']);   // 'abcd'
x(['1','2']);           // '12'
x([undefined,null]);    // ''

Is there any way to write this function using even less characters? 有什么方法可以使用更少的字符来编写此函数?

借助ES6的模板文字,您可以这样编写:

x=a=>a.join``

如果您愿意使用default ,分隔符:

x=a=>a+''

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

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