Array.prototype.join()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Array 实例的 join() 方法通过连接此数组中的所有元素(用逗号或指定的分隔符字符串分隔)来创建并返回一个新字符串。如果数组只有一项,则将返回该项而不使用分隔符。

¥The join() method of Array instances creates and returns a new string by concatenating all of the elements in this array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Try it

语法

¥Syntax

js
join()
join(separator)

参数

¥Parameters

separator Optional

用于分隔数组的每对相邻元素的字符串。如果省略,则数组元素用逗号 (",") 分隔。

返回值

¥Return value

所有数组元素连接在一起的字符串。如果 array.length0,则返回空字符串。

¥A string with all array elements joined. If array.length is 0, the empty string is returned.

描述

¥Description

所有数组元素的字符串转换都连接成一个字符串。如果元素是 undefinednull,则会将其转换为空字符串,而不是字符串 "null""undefined"

¥The string conversions of all array elements are joined into one string. If an element is undefined or null, it is converted to an empty string instead of the string "null" or "undefined".

join 方法由 Array.prototype.toString() 在内部访问,不带任何参数。覆盖数组实例的 join 也会覆盖其 toString 行为。

¥The join method is accessed internally by Array.prototype.toString() with no arguments. Overriding join of an array instance will override its toString behavior as well.

Array.prototype.join 递归地将每个元素(包括其他数组)转换为字符串。由于 Array.prototype.toString 返回的字符串(与调用 join() 相同)没有分隔符,因此嵌套数组看起来像是被展平的。你只能控制第一级的分隔符,而更深的级别始终使用默认的逗号。

¥Array.prototype.join recursively converts each element, including other arrays, to strings. Because the string returned by Array.prototype.toString (which is the same as calling join()) does not have delimiters, nested arrays look like they are flattened. You can only control the separator of the first level, while deeper levels always use the default comma.

js
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(matrix.join()); // 1,2,3,4,5,6,7,8,9
console.log(matrix.join(";")); // 1,2,3;4,5,6;7,8,9

当数组是循环的(它包含本身的元素)时,浏览器通过忽略循环引用来避免无限递归。

¥When an array is cyclic (it contains an element that is itself), browsers avoid infinite recursion by ignoring the cyclic reference.

js
const arr = [];
arr.push(1, [3, arr, 4], 2);
console.log(arr.join(";")); // 1;3,,4;2

当在 稀疏数组 上使用时,join() 方法会迭代空槽,就好像它们具有值 undefined 一样。

¥When used on sparse arrays, the join() method iterates empty slots as if they have the value undefined.

join() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。

¥The join() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

连接数组有四种不同的方式

¥Joining an array four different ways

以下示例创建一个包含三个元素的数组 a,然后连接该数组四次:使用默认分隔符,然后是逗号和空格,然后是加号和空字符串。

¥The following example creates an array, a, with three elements, then joins the array four times: using the default separator, then a comma and a space, then a plus and an empty string.

js
const a = ["Wind", "Water", "Fire"];
a.join(); // 'Wind,Water,Fire'
a.join(", "); // 'Wind, Water, Fire'
a.join(" + "); // 'Wind + Water + Fire'
a.join(""); // 'WindWaterFire'

在稀疏数组上使用 join()

¥Using join() on sparse arrays

join() 对待空槽的方式与 undefined 相同,并产生一个额外的分隔符:

¥join() treats empty slots the same as undefined and produces an extra separator:

js
console.log([1, , 3].join()); // '1,,3'
console.log([1, undefined, 3].join()); // '1,,3'

对非数组对象调用 join()

¥Calling join() on non-array objects

join() 方法读取 thislength 属性,然后访问键为小于 length 的非负整数的每个属性。

¥The join() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
  3: 5, // ignored by join() since length is 3
};
console.log(Array.prototype.join.call(arrayLike));
// 2,3,4
console.log(Array.prototype.join.call(arrayLike, "."));
// 2.3.4

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.join

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看