Array
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
* Some parts of this feature may have varying levels of support.
JavaScript ä¸ç Array å
¨åç©ä»¶è¢«ç¨æ¼å»ºæ§é£åï¼é£åçºé«éï¼high-levelï¼ãä¼¼å表ï¼list-likeï¼çç©ä»¶ãé£åå¨ Javascript 裡é¢ä¸¦æ²æåºå®çé·åº¦èåå¥ãç±æ¼é£åçé·åº¦å¯ä»¥é¨æè¢«æ¹è®ï¼æä»¥ä¸¦ä¸è½ä¿èé£åçå¯åº¦ãéåæ±ºæ¼éç¼è
å¦ä½ä½¿ç¨é£åãä¸è¬ä¾èªªï¼éæ¯åé常æ¹ä¾¿çç¹æ§ï¼ä½å¦æé並ä¸é©ç¨æ¼ä½ çéç¼å·¥ä½ï¼ä½ ä¹è¨±æèæ
®ä½¿ç¨åå¥é£åã
èªæ³
[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)
忏
elementN-
é¤äºåªå³éä¸ååæ¸çµ¦
Arrayæ§é 彿¸ï¼ä¸è©²åæ¸çºä¸åæ¸åçæ æ³ï¼è©³è¦ä¸æ¹ç arrayLength 忏ï¼ï¼JavaScript é£åæä»¥å³å ¥çå ç´ é²è¡åå§åãè«æ³¨æï¼éç¨®ç¹æ®æ æ³å é©ç¨æ¼ä»¥Arrayæ§é 彿¸å»ºç«ç JavaScript é£åï¼èä¸é©ç¨æ¼ä»¥æ¬èèªæ³å»ºç«çé£å常å¼ï¼Array Literalsï¼ã arrayLength-
妿å³é給
Arrayæ§é 彿¸çå¯ä¸åæ¸æ¯ 0 å 2^32 - 1ï¼å«ï¼ä¹éçæ´æ¸ï¼å°åå³ä¸åæ°ç JavaScript é£åï¼å ¶é·åº¦è¢«è¨å®çºé忏åã妿忏æ¯ä»»ä½å ¶ä»æ¸å¼ï¼å°æåºRangeErrorç°å¸¸ã
說æ
Arrayï¼ãé£åãï¼æ¯é¡ä¼¼å表ï¼listï¼çç©ä»¶ï¼Objectï¼ï¼å®åçååï¼Prototypeï¼æææ¹æ³ï¼methodsï¼ä¾å·è¡éæ·åè®ç°æä½ãJavaScript é£åçé·åº¦ï¼å ç´ æ¸éï¼ï¼ä»¥åå ¶å ç´ çé¡åé½ä¸æ¯åºå®çãåæ±ºæ¼å·¥ç¨å¸«å¦ä½é¸æä½¿ç¨é£åï¼å¯ä»¥é¨ææ´æ¹é£åçé·åº¦ï¼ä¹å¯ä¸é£çºå²åè³æï¼ æä»¥ä¸¦ä¸ä¿èéäºè³ææ¯éä¸çãä¸è¬æ æ³ä¸ï¼éäºç¹æ§å¾æ¹ä¾¿ä½¿ç¨ï¼ä½è¥éäºåè½é½ä¸ç¬¦åä½ çç¨éï¼ä½ å¯è½ææ³ä½¿ç¨åå¥é£åï¼typed arraysï¼ã
æäºäººèªçºå³ä¾¿æç¼çè¦åï¼ä»ç¶ä¸æè©²ä½¿ç¨éè¯é£åï¼èæè©²ä½¿ç¨ objectsãä½ å¯åèè¼éç´ JavaScript åå
¸ç¶ä¸çç¯ä¾ã
ååé£åå ç´
JavaScript é£åæ¯ zero-indexedï¼é£åå
ç´ çç´¢å¼å¼ç·¨æå¾ 0 éå§ï¼èæå¾ä¸åå
ç´ çç´¢å¼å¼çåæ¼é£åç length å±¬æ§æ¸ 1ã
var arr = ["this is the first element", "this is the second element"];
console.log(arr[0]); // ç´éåº 'this is the first element'
console.log(arr[1]); // è¨éåº 'this is the second element'
console.log(arr[arr.length - 1]); // è¨éåº 'this is the second element'
Array å
ç´ åæä¹æ¯ç©ä»¶ç屬æ§ï¼è toString æ¯ä¸ç¨®å±¬æ§ç¸åãä½è¥è¦ééä¸é¢é種æ¹å¼ååé£åå
ç´ ï¼å çºå±¬æ§åç¨±ç¡æçéä¿ï¼æç¼çèªæ³é¯èª¤ï¼
console.log(arr.0); // èªæ³é¯èª¤
æé æå¦æ¤çåå æ²æä»éº¼ç¹å¥çï¼å¨ JavaScript ç¶ä¸ç¡æ³ç¨å°æ¸é»çæ¹å¼ä¾åç
§ä¸åå稱éé çºæ¸åç屬æ§ï¼èå¿
é æ¬èç表示æ¹å¼ä¾ååãèä¾ä¾èªªï¼è¥ä½ æåç©ä»¶ç屬æ§å稱çºã3dãï¼å°±åªè½ç¨æ¬èçæ¹å¼ä¾åç
§ã
è«çä¸åç¯ä¾ï¼
var years = [1950, 1960, 1970, 1980, 1990, 2000, 2010];
console.log(years.0); // èªæ³é¯èª¤
console.log(years[0]); // ç¨å¼æ£å¸¸
renderer.3d.setTexture(model, 'character.png'); // èªæ³é¯èª¤
renderer['3d'].setTexture(model, 'character.png'); // ç¨å¼æ£å¸¸
注æï¼ä»¥éå '3d' ä¾åä¾èªªï¼å¿
é ç¨å¼èå° 3d å
èµ·ä¾ãä½ ä¹å¯ä»¥å° JavaScript é£åçç´¢å¼ç¨å¼èå
èµ·ä¾ï¼ä¾å¦ä½¿ç¨ years['2'] èä¸ç¨ years[2]ï¼ï¼ä½é䏿¯å¿
è¦çãJavaScript æééé±å«ç toStringï¼å° years[2] ç¶ä¸ç 2 å¼·å¶è½æçºå串ãç±æ¼éååå ï¼'2' è '02' æåç
§å° years ç©ä»¶ä¸çä¸åé
ç®ï¼ä¸åç¨å¼ç¯ä¾çµæå¯è½åå³ trueï¼
console.log(years["2"] != years["02"]);
å¦ä¸ç¨®é¡ä¼¼çæ æ³æ¯ï¼ç©ä»¶å±¬æ§å好èä¿çåï¼ï¼ï¼ç¸åçæ æ³ãéç¨®æ æ³ä¸å è½é鿬è表示æ¹å¼ç¶ä¸çå串常å¼ä¾ååï¼
var promise = {
var: "text",
array: [1, 2, 3, 4],
};
console.log(promise["var"]);
length èæ¸å¼å±¬æ§çéä¿
JavaScript é£åç length 屬æ§åå
¶æ¸å¼å±¬æ§ç¸éã許å¤é£åçæ¹æ³è¢«å¼å«ææåè length 屬æ§çå¼ï¼ä¾å¦ joinãsliceãindexOf çï¼ãèæå¦ä¸äºæ¹æ³åæå»æ¹è® length 屬æ§çå¼ï¼å¦ pushãspliceã
var fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
å¦æçµ¦é£åè¨å®ä¸åæ¸å¼å±¬æ§ï¼å
¶å¼çºææä½è¶
éç¶ä¸ç¯åçé£å indexï¼JavaScript 弿æä¾ç
§æ¤æ¸å¼æ´æ°é£åç length 屬æ§ï¼
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
æé« length 屬æ§ã
fruits.length = 10;
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
éä½ length 屬æ§åæåªé¤é£åå
ç´ ã
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
å¨ Array.length é é¢è£¡æé²ä¸æ¥è§£éã
ä½¿ç¨ match åå³çµæä¾å»ºç«é£å
å¨åä¸²èæ£è¦è¡¨ç¤ºå¼ä¹éçæ¯å°çµææç¢çä¸å javascript é£åãæ¤é£åå
§å«éæ¼æ¯å°è³è¨ç屬æ§èå
ç´ ã 鿍£çé£åç±RegExp.exec, String.match, å String.replace æç¢çãåè以ä¸ç¯ä¾åè¡¨æ ¼ï¼ææå©æ¼èªªæéäºå±¬æ§åå
ç´ ï¼
// æ¯å°ä¸ååå
dï¼å¾é¢æ¥è䏿å¤å bï¼åæ¥èä¸å d
// Remember matched b's and the following d
// 忽ç¥å¤§å°å¯«
var myRe = /d(b+)(d)/i;
var myArray = myRe.exec("cdbBdbsbz");
éé æ¯å°çµæç屬æ§èå ç´ åèå¦ä¸ï¼
| 屬æ§/å ç´ | 說æ | ç¯ä¾ |
|---|---|---|
input |
å¯è®å±¬æ§ï¼ä»£è¡¨ æ£è¦è¡¨ç¤ºå¼ç¨ä»¥æ¯å°çåå§å串ã | cdbBdbsbz |
index |
å¯è®å±¬æ§ï¼ä»£è¡¨å¨å䏲䏿¯å°å¾å°çç´¢å¼ï¼æ¯ä»¥é¶çºåºç¤(å¾ 0 éå§)ã | 1 |
[0] |
ä¸åå¯ç¨å ç´ ä»¥è¡¨ç¤ºæå¾ç¬¦åçå串 | dbBd |
[1], ...[n] |
Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. | [1]: bB [2]: d |
屬æ§
- Array.length
-
Array建æ§åçé·åº¦çº 1ã Array[Symbol.species]-
ç¨ä¾å»ºç«è¡çç©ä»¶ç建æ§å½æ¸ã
Array.prototype-
å¯å å ¥å±¬æ§è³ææé£åç©ä»¶ã
æ¹æ³
Array.from()-
ç¨é¡ä¼¼é£åæå¯åèç©ä»¶ï¼ä¾å»ºç«æ°ç
Array實ä¾ã Array.isArray()-
è¥è®æ¸æ¯é£åå°±åå³ trueï¼å¦ååå³ falseã
Array.of()-
ç¨å¯è®æ¸éç弿¸ä¾å»ºç«æ°ç
Array實ä¾ï¼ä¸è«å¼æ¸çæ¸éæåå¥ã
Array 實ä¾
ææçé£å實ä¾é½ç¹¼æ¿èª Array.prototypeãè¥ä¿®æ¹éåé£å建æ§å (Array constructor) çååç©ä»¶ (prototype object)ï¼å°æå½±é¿ææçé£å實é«ã
屬æ§
Array.prototype.length-
Reflects the number of elements in an array.
Array.prototype[Symbol.unscopables]-
Contains property names that were not included in the ECMAScript standard prior to the ES2015 version and that are ignored for
withstatement-binding purposes.
æ¹æ³
Array.prototype.at()-
Returns the array item at the given index. Accepts negative integers, which count back from the last item.
Array.prototype.concat()-
Returns a new array that is the calling array joined with other array(s) and/or value(s).
Array.prototype.copyWithin()-
Copies a sequence of array elements within an array.
Array.prototype.entries()-
Returns a new array iterator object that contains the key/value pairs for each index in an array.
Array.prototype.every()-
Returns
trueif every element in the calling array satisfies the testing function. Array.prototype.fill()-
Fills all the elements of an array from a start index to an end index with a static value.
Array.prototype.filter()-
Returns a new array containing all elements of the calling array for which the provided filtering function returns
true. Array.prototype.find()-
Returns the value of the first element in the array that satisfies the provided testing function, or
undefinedif no appropriate element is found. Array.prototype.findIndex()-
Returns the index of the first element in the array that satisfies the provided testing function, or
-1if no appropriate element was found. Array.prototype.findLast()-
Returns the value of the last element in the array that satisfies the provided testing function, or
undefinedif no appropriate element is found. Array.prototype.findLastIndex()-
Returns the index of the last element in the array that satisfies the provided testing function, or
-1if no appropriate element was found. Array.prototype.flat()-
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Array.prototype.flatMap()-
Returns a new array formed by applying a given callback function to each element of the calling array, and then flattening the result by one level.
Array.prototype.forEach()-
Calls a function for each element in the calling array.
Array.prototype.group()-
Groups the elements of an array into an object according to the strings returned by a test function.
Array.prototype.groupToMap()-
Groups the elements of an array into a
Mapaccording to values returned by a test function. Array.prototype.includes()-
Determines whether the calling array contains a value, returning
trueorfalseas appropriate. Array.prototype.indexOf()-
Returns the first (least) index at which a given element can be found in the calling array.
Array.prototype.join()-
Joins all elements of an array into a string.
Array.prototype.keys()-
Returns a new array iterator that contains the keys for each index in the calling array.
Array.prototype.lastIndexOf()-
Returns the last (greatest) index at which a given element can be found in the calling array, or
-1if none is found. Array.prototype.map()-
Returns a new array containing the results of invoking a function on every element in the calling array.
Array.prototype.pop()-
Removes the last element from an array and returns that element.
Array.prototype.push()-
Adds one or more elements to the end of an array, and returns the new
lengthof the array. Array.prototype.reduce()-
Executes a user-supplied "reducer" callback function on each element of the array (from left to right), to reduce it to a single value.
Array.prototype.reduceRight()-
Executes a user-supplied "reducer" callback function on each element of the array (from right to left), to reduce it to a single value.
Array.prototype.reverse()-
Reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)
Array.prototype.shift()-
Removes the first element from an array and returns that element.
Array.prototype.slice()-
Extracts a section of the calling array and returns a new array.
Array.prototype.some()-
Returns
trueif at least one element in the calling array satisfies the provided testing function. Array.prototype.sort()-
Sorts the elements of an array in place and returns the array.
Array.prototype.splice()-
Adds and/or removes elements from an array.
Array.prototype.toLocaleString()-
Returns a localized string representing the calling array and its elements. Overrides the
Object.prototype.toLocaleString()method. Array.prototype.toString()-
Returns a string representing the calling array and its elements. Overrides the
Object.prototype.toString()method. Array.prototype.unshift()-
Adds one or more elements to the front of an array, and returns the new
lengthof the array. Array.prototype.values()-
Returns a new array iterator object that contains the values for each index in the array.
Array.prototype[Symbol.iterator]()-
An alias for the
values()method by default.
ç¯ä¾
>ç¯ä¾ï¼å»ºç«é£å
以ä¸ç¯ä¾æç¢çé·åº¦çº 0 ç msgArray é£åï¼ç¶å¾ææ´¾å串å¼å° msgArray[0] å msgArray[99]ï¼ä½¿é£åçé·åº¦è®çº 100ã
var msgArray = [];
msgArray[0] = "Hello";
msgArray[99] = "world";
if (msgArray.length === 100) {
console.log("The length is 100.");
}
ï¼ééç´¢å¼ï¼åå¾é£åé ç®
var first = fruits[0];
// Apple
var last = fruits[fruits.length - 1];
// Banana
è¿ä»£é£å
fruits.forEach(function (item, index, array) {
console.log(item, index);
});
// Apple 0
// Banana 1
å å ¥é ç®è³é£åæ«ç«¯
var newLength = fruits.push("Orange");
// ["Apple", "Banana", "Orange"]
ç§»é¤é£åæ«ç«¯é ç®
var last = fruits.pop(); // ç§»é¤ï¼ææ«ç«¯çï¼Orange
// ["Apple", "Banana"];
ç§»é¤é£åå端é ç®
var first = fruits.shift(); // ç§»é¤ï¼æå端çï¼Apple
// ["Banana"];
å å ¥é ç®è³é£åå端
var newLength = fruits.unshift("Strawberry"); // å å°é£åå端
// ["Strawberry", "Banana"];
å¨é£åä¸å°æ¾é ç®çç´¢å¼
fruits.push("Mango");
// ["Strawberry", "Banana", "Mango"]
var pos = fruits.indexOf("Banana");
// 1
ç§»é¤æå®ç´¢å¼ä½ç½®çé ç®
var removedItem = fruits.splice(pos, 1); // ç§»é¤ pos èµ·ç 1 åé
ç®
// ["Strawberry", "Mango"]
ç§»é¤æå®ç´¢å¼ä½ç½®èµ·çå¤åé ç®
var vegetables = ["Cabbage", "Turnip", "Radish", "Carrot"];
console.log(vegetables);
// ["Cabbage", "Turnip", "Radish", "Carrot"]
var pos = 1,
n = 2;
var removedItems = vegetables.splice(pos, n);
// éå°±æ¯ç§»é¤é
ç®çæ¹å¼ï¼
// n 表示å¾è©²ä½ç½®éå§ï¼ä¸ç´å°é£åç尾端æå¤å°é
ç®éè¦ç§»é¤
console.log(vegetables);
// ["Cabbage", "Carrot"]ï¼åå§çé£å被æ¹è®ï¼
console.log(removedItems);
// ["Turnip", "Radish"]
è¤è£½é£å
var shallowCopy = fruits.slice(); // éå°±æ¯è¤è£½é£åçæ¹å¼
// ["Strawberry", "Mango"]
建ç«äºç¶é£å
以ä¸ç¯ä¾æç¨å串ç¢çä¸å¼µè¥¿æ´æ£ç¤çäºç¶é£åãç¬¬ä¸æ¥æ¯å°å£«å µ 'p' å¾ (6,4) ç§»åè³ (4,4)ï¼ç¶å¾æ¸ ç©ºåæ¬çä½ç½® (6,4)ã
var board = [
["R", "N", "B", "Q", "K", "B", "N", "R"],
["P", "P", "P", "P", "P", "P", "P", "P"],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
["p", "p", "p", "p", "p", "p", "p", "p"],
["r", "n", "b", "q", "k", "b", "n", "r"],
];
console.log(board.join("\n") + "\n\n");
// å°å£«å
µå¾åç§»å
©æ¥
board[4][4] = board[6][4];
board[6][4] = " ";
console.log(board.join("\n"));
以䏿¯è¼¸åºçµæï¼
R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , , , , , , , , , , , , p,p,p,p,p,p,p,p r,n,b,q,k,b,n,r R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , ,p, , , , , , , , , , p,p,p,p, ,p,p,p r,n,b,q,k,b,n,r
使ç¨é£åä¾ä»¥è¡¨æ ¼é¡¯ç¤ºå¤åæ¸å¼
values = [];
for (var x = 0; x < 10; x++) {
values.push([2 ** x, 2 * x ** 2]);
}
console.table(values);
çµæææ¯
0 1 0 1 2 2 2 4 8 3 8 18 4 16 32 5 32 50 6 64 72 7 128 98 8 256 128 9 512 162
ï¼ç¬¬ä¸æ¬çºç´¢å¼ï¼
è¦ç¯
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-array-objects> |