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
本文深入探讨了JavaScript中Array.prototype.join方法的使用,解释了如何利用该方法将数组元素转换为字符串并连接,包括分隔符的使用、null与undefined的处理方式以及被删除元素的表现。
2万+

被折叠的 条评论
为什么被折叠?



