Skip to content

API Array

Liu.Yandong.Hanks edited this page Aug 19, 2026 · 3 revisions

Array

可增长的通用数组

Growable general-purpose array

脚本 API 目录

Script API Directory

概述

Overview

Array 保存异构元素并支持动态属性和增长。已知长度、仅做数值循环的工作负载应优先考虑 Int32ArrayFloat64ArrayInt8ArrayBooleanArrayArray 适合需要灵活元素类型和常见集合操作的场景。

Array stores heterogeneous elements and supports dynamic properties and growth. For known-size numeric loops, prefer Int32Array, Float64Array, Int8Array, or BooleanArray; use Array when flexible element types and common collection operations are needed.

new Array([capacity])

Parameters:

  • capacity
    可选预留容量;不创建 capacity 个空槽。

    Optional reserved capacity; does not create capacity null slots.

Returns

空数组。

Empty array.

var items = new Array(8);
items.push("first");
return items.length; // 1

Array.from(iterable, [callback])

Parameters:

  • iterable
    可枚举值。

    Iterable value.

  • callback
    可选映射回调。

    Optional mapping callback.

Returns

新数组。

New array.

return Array.from([1, 2, 3], value => value * 2).join(","); // 2,4,6

Array.isArray(value)

Parameters:

  • value
    待检查值。

    Value to inspect.

Returns

是否为一般 Array;Packed Array 返回 false

Whether it is a general Array; packed arrays return false.

return Array.isArray([1, 2]); // true

Array.of(...items)

Parameters:

  • items
    零个或多个元素。

    Zero or more elements.

Returns

包含这些元素的新数组。

New array containing the elements.

return Array.of("a", "b").length; // 2

Array.withCapacity([capacity])

Parameters:

  • capacity
    可选预留容量。

    Optional reserved capacity.

Returns

空数组。

Empty array.

用途

已知后续会大量 push 时减少扩容。

Reduces growth when many later push calls are known.

var values = Array.withCapacity(100);
values.push(1);
return values.length; // 1

items.length

Returns

当前元素数。

Current element count.

return ["a", "b"].length; // 2

items.has(value)

Parameters:

  • value
    要搜索的值。

    Value to search for.

Returns

是否包含该值。

Whether the value is present.

return [1, 2, 3].has(2); // true

items.indexOf(value, [fromIndex])

Parameters:

  • value
    要搜索的值。

    Value to search for.

  • fromIndex
    可选起始索引。

    Optional starting index.

Returns

第一次匹配索引;未找到时为负值。

First matching index, or a negative value when missing.

return [1, 2, 2].indexOf(2); // 1

items.lastIndexOf(value, [fromIndex])

Parameters:

  • value
    要搜索的值。

    Value to search for.

  • fromIndex
    可选反向搜索起点。

    Optional reverse-search starting index.

Returns

最后一次匹配索引;未找到时为负值。

Last matching index, or a negative value when missing.

return [1, 2, 2].lastIndexOf(2); // 2

items.push(...values)

Parameters:

  • values
    一个或多个待追加元素。

    One or more elements to append.

Returns

新长度。

New length.

副作用

原地修改数组。

Mutates the array.

var items = [1];
return items.push(2, 3); // 3

items.pop()

Parameters:

  • 无。

    None.

Returns

最后一项;空数组时为 null

Last item, or null for an empty array.

副作用

原地删除最后一项。

Removes the last item in place.

var items = [1, 2];
return items.pop(); // 2

items.unshift(...values)

Parameters:

  • values
    一个或多个待插入元素。

    One or more elements to prepend.

Returns

新长度。

New length.

副作用

原地移动现有元素。

Shifts existing elements in place.

var items = [2];
items.unshift(0, 1);
return items.join(","); // 0,1,2

items.shift()

Parameters:

  • 无。

    None.

Returns

第一项;空数组时为 null

First item, or null for an empty array.

副作用

原地删除第一项。

Removes the first item in place.

var items = [1, 2];
return items.shift(); // 1

items.concat(...values)

Parameters:

  • values
    值或数组。

    Values or arrays.

Returns

新数组,原数组不变。

New array; the original array is unchanged.

return [1].concat([2, 3], 4).join(","); // 1,2,3,4

items.slice(start, [end])

Parameters:

  • start
    起始索引。

    Starting index.

  • end
    可选结束索引。

    Optional end index.

Returns

浅拷贝的新数组。

Shallow-copied new array.

return [0, 1, 2].slice(1).join(","); // 1,2

items.reverse()

Parameters:

  • 无。

    None.

Returns

当前数组。

This array.

副作用

原地反转。

Reverses in place.

var items = [1, 2, 3];
return items.reverse().join(","); // 3,2,1

items.sort()

Parameters:

  • 无。

    None.

Returns

当前数组。

This array.

副作用

使用运行时默认比较器原地排序。

Sorts in place with the runtime default comparer.

var items = [3, 1, 2];
return items.sort().join(","); // 1,2,3

items.join([separator])

Parameters:

  • separator
    可选分隔文本。

    Optional separator text.

Returns

拼接后的字符串。

Joined string.

return ["a", "b", "c"].join("-"); // a-b-c

items.find(callback)

Parameters:

  • callback(value, index)
    返回真值时选中元素的函数。

    Function selecting an element by returning a truthy value.

Returns

第一个匹配值;没有匹配时为 null

First matching value, or null when none match.

return [1, 4, 7].find(value => value > 3); // 4

items.findIndex(callback)

Parameters:

  • callback(value, index)
    谓词函数。

    Predicate function.

Returns

第一个匹配索引;没有匹配时为负值。

First matching index, or a negative value when none match.

return [1, 4, 7].findIndex(value => value > 3); // 1

items.findLast(callback)

Parameters:

  • callback(value, index)
    谓词函数。

    Predicate function.

Returns

最后一个匹配值;没有匹配时为 null

Last matching value, or null when none match.

return [1, 4, 7].findLast(value => value > 3); // 7

items.findLastIndex(callback)

Parameters:

  • callback(value, index)
    谓词函数。

    Predicate function.

Returns

最后一个匹配索引;没有匹配时为负值。

Last matching index, or a negative value when none match.

return [1, 4, 7].findLastIndex(value => value > 3); // 2

items.map(callback)

Parameters:

  • callback(value, index)
    映射函数。

    Mapping function.

Returns

新数组。

New array.

return [1, 2].map(value => value * 10).join(","); // 10,20

items.filter(callback)

Parameters:

  • callback(value, index)
    筛选谓词。

    Filter predicate.

Returns

包含匹配项的新数组。

New array containing matching items.

return [1, 2, 3].filter(value => value % 2 == 1).join(","); // 1,3

items.some(callback)

Parameters:

  • callback(value, index)
    谓词函数。

    Predicate function.

Returns

是否有任一元素匹配。

Whether any item matches.

return [1, 2, 3].some(value => value > 2); // true

items.every(callback)

Parameters:

  • callback(value, index)
    谓词函数。

    Predicate function.

Returns

是否所有元素匹配。

Whether every item matches.

return [1, 2, 3].every(value => value > 0); // true

items.flat([depth])

Parameters:

  • depth
    可选展开层数,缺省为 1

    Optional flattening depth; defaults to 1.

Returns

新数组。

New array.

return [1, [2, [3]]].flat(2).join(","); // 1,2,3

items.reduce(callback)

Parameters:

  • callback(accumulator, value, index)
    累积函数。

    Accumulator function.

Returns

累积结果;空数组时为 null

Accumulated result, or null for an empty array.

边界行为

第一个元素自动作为初始累加器。

The first element is automatically the initial accumulator.

return [1, 2, 3].reduce((sum, value) => sum + value); // 6

Clone this wiki locally