Array.prototype.join
将数组里的每一个元素都转换为字符串并拼接在一起,带有唯一参数:分隔符,如果省略,则分隔符默认为逗号.
null
, undefined
, 以及被删除的元素将成为拼接字符串里的空字符串(empty string):
const arr = [1, null, "hello", "world", true, undefined];
delete arr[3]; // 删除 "world"
arr.join(); // 分隔符为逗号: "1,,hello,,true,"
arr.join(''); // "1hellotrue"
arr.join(' -- '); // "1 -- -- hello -- -- true --"
[1] Learning JavaScript Chapter 8 String Joining