-
Notifications
You must be signed in to change notification settings - Fork 0
API Array
可增长的通用数组
Growable general-purpose array
Overview
Array 保存异构元素并支持动态属性和增长。已知长度、仅做数值循环的工作负载应优先考虑 Int32Array、Float64Array、Int8Array 或 BooleanArray;Array 适合需要灵活元素类型和常见集合操作的场景。
Arraystores heterogeneous elements and supports dynamic properties and growth. For known-size numeric loops, preferInt32Array,Float64Array,Int8Array, orBooleanArray; useArraywhen flexible element types and common collection operations are needed.
Parameters:
-
capacity
可选预留容量;不创建capacity个空槽。Optional reserved capacity; does not create
capacitynull slots.
Returns
空数组。
Empty array.
var items = new Array(8);
items.push("first");
return items.length; // 1Parameters:
-
iterable
可枚举值。Iterable value.
-
callback
可选映射回调。Optional mapping callback.
Returns
新数组。
New array.
return Array.from([1, 2, 3], value => value * 2).join(","); // 2,4,6Parameters:
-
value
待检查值。Value to inspect.
Returns
是否为一般 Array;Packed Array 返回 false。
Whether it is a general
Array; packed arrays returnfalse.
return Array.isArray([1, 2]); // trueParameters:
-
items
零个或多个元素。Zero or more elements.
Returns
包含这些元素的新数组。
New array containing the elements.
return Array.of("a", "b").length; // 2Parameters:
-
capacity
可选预留容量。Optional reserved capacity.
Returns
空数组。
Empty array.
用途
已知后续会大量 push 时减少扩容。
Reduces growth when many later
pushcalls are known.
var values = Array.withCapacity(100);
values.push(1);
return values.length; // 1Returns
当前元素数。
Current element count.
return ["a", "b"].length; // 2Parameters:
-
value
要搜索的值。Value to search for.
Returns
是否包含该值。
Whether the value is present.
return [1, 2, 3].has(2); // trueParameters:
-
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); // 1Parameters:
-
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); // 2Parameters:
-
values
一个或多个待追加元素。One or more elements to append.
Returns
新长度。
New length.
副作用
原地修改数组。
Mutates the array.
var items = [1];
return items.push(2, 3); // 3Parameters:
- 无。
None.
Returns
最后一项;空数组时为 null。
Last item, or
nullfor an empty array.
副作用
原地删除最后一项。
Removes the last item in place.
var items = [1, 2];
return items.pop(); // 2Parameters:
-
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,2Parameters:
- 无。
None.
Returns
第一项;空数组时为 null。
First item, or
nullfor an empty array.
副作用
原地删除第一项。
Removes the first item in place.
var items = [1, 2];
return items.shift(); // 1Parameters:
-
values
值或数组。Values or arrays.
Returns
新数组,原数组不变。
New array; the original array is unchanged.
return [1].concat([2, 3], 4).join(","); // 1,2,3,4Parameters:
-
start
起始索引。Starting index.
-
end
可选结束索引。Optional end index.
Returns
浅拷贝的新数组。
Shallow-copied new array.
return [0, 1, 2].slice(1).join(","); // 1,2Parameters:
- 无。
None.
Returns
当前数组。
This array.
副作用
原地反转。
Reverses in place.
var items = [1, 2, 3];
return items.reverse().join(","); // 3,2,1Parameters:
- 无。
None.
Returns
当前数组。
This array.
副作用
使用运行时默认比较器原地排序。
Sorts in place with the runtime default comparer.
var items = [3, 1, 2];
return items.sort().join(","); // 1,2,3Parameters:
-
separator
可选分隔文本。Optional separator text.
Returns
拼接后的字符串。
Joined string.
return ["a", "b", "c"].join("-"); // a-b-cParameters:
-
callback(value, index)
返回真值时选中元素的函数。Function selecting an element by returning a truthy value.
Returns
第一个匹配值;没有匹配时为 null。
First matching value, or
nullwhen none match.
return [1, 4, 7].find(value => value > 3); // 4Parameters:
-
callback(value, index)
谓词函数。Predicate function.
Returns
第一个匹配索引;没有匹配时为负值。
First matching index, or a negative value when none match.
return [1, 4, 7].findIndex(value => value > 3); // 1Parameters:
-
callback(value, index)
谓词函数。Predicate function.
Returns
最后一个匹配值;没有匹配时为 null。
Last matching value, or
nullwhen none match.
return [1, 4, 7].findLast(value => value > 3); // 7Parameters:
-
callback(value, index)
谓词函数。Predicate function.
Returns
最后一个匹配索引;没有匹配时为负值。
Last matching index, or a negative value when none match.
return [1, 4, 7].findLastIndex(value => value > 3); // 2Parameters:
-
callback(value, index)
映射函数。Mapping function.
Returns
新数组。
New array.
return [1, 2].map(value => value * 10).join(","); // 10,20Parameters:
-
callback(value, index)
筛选谓词。Filter predicate.
Returns
包含匹配项的新数组。
New array containing matching items.
return [1, 2, 3].filter(value => value % 2 == 1).join(","); // 1,3Parameters:
-
callback(value, index)
谓词函数。Predicate function.
Returns
是否有任一元素匹配。
Whether any item matches.
return [1, 2, 3].some(value => value > 2); // trueParameters:
-
callback(value, index)
谓词函数。Predicate function.
Returns
是否所有元素匹配。
Whether every item matches.
return [1, 2, 3].every(value => value > 0); // trueParameters:
-
depth
可选展开层数,缺省为1。Optional flattening depth; defaults to
1.
Returns
新数组。
New array.
return [1, [2, [3]]].flat(2).join(","); // 1,2,3Parameters:
-
callback(accumulator, value, index)
累积函数。Accumulator function.
Returns
累积结果;空数组时为 null。
Accumulated result, or
nullfor an empty array.
边界行为
第一个元素自动作为初始累加器。
The first element is automatically the initial accumulator.
return [1, 2, 3].reduce((sum, value) => sum + value); // 6AuroraScript.JIT 4.0.0 · Documentation Home · Repository · MIT License