- Object/Oriented JavaScript
- Stoyan Stefanov
- 635字
- 2021-08-13 19:25:52
Arrays
Now that you know the basic primitive data types in JavaScript, it's time to move to a more interesting data structure—the array.
To declare a variable that contains an empty array, you use square brackets with nothing between them:
>>> var a = []; >>> typeof a;
"object"
typeof
returns "object", but don't worry about this for the time being, we'll get to that when we take a closer look at objects.
To define an array that has three elements, you do this:
>>> var a = [1,2,3];
When you simply type the name of the array in the Firebug console, it prints the contents of the array:
>>> a
[1, 2, 3]
So what is an array exactly? It's simply a list of values. Instead of using one variable to store one value, you can use one array variable to store any number of values as elements of the array. Now the question is how to access each of these stored values?
The elements contained in an array are indexed with consecutive numbers starting from zero. The first element has index (or position) 0, the second has index 1 and so on. Here's the three-element array from the previous example:

In order to access an array element, you specify the index of that element inside square brackets. So a[0]
gives you the first element of the array a
, a[1]
gives you the second, and so on.
>>> a[0]
1
>>> a[1]
2
Adding/Updating Array Elements
Using the index, you can also update elements of the array. The next example updates the third element (index 2) and prints the contents of the new array.
>>> a[2] = 'three';
"three"
>>> a
[1, 2, "three"]
You can add more elements, by addressing an index that didn't exist before.
>>> a[3] = 'four';
"four"
>>> a
[1, 2, "three", "four"]
If you add a new element, but leave a gap in the array, those elements in between are all assigned the undefined
value. Check out this example:
>>> var a = [1,2,3]; >>> a[6] = 'new';
"new"
>>> a
[1, 2, 3, undefined, undefined, undefined, "new"]
Deleting Elements
In order to delete an element, you can use the delete
operator. It doesn't actually remove the element, but sets its value to undefined
. After the deletion, the length of the array does not change.
>>> var a = [1, 2, 3]; >>> delete a[1];
true
>>> a
[1, undefined, 3]
Arrays of arrays
An array can contain any type of values, including other arrays.
>>> var a = [1, "two", false, null, undefined]; >>> a
[1, "two", false, null, undefined]
>>> a[5] = [1,2,3]
[1, 2, 3]
>>> a
[1, "two", false, null, undefined, [1, 2, 3]]
Let's see an example where you have an array of two elements, each of them being an array.
>>> var a = [[1,2,3],[4,5,6]]; >>> a
[[1, 2, 3], [4, 5, 6]]
The first element of the array is a[0]
and it is an array itself.
>>> a[0]
[1, 2, 3]
To access an element in the nested array, you refer to the element index in another set of square brackets.
>>> a[0][0]
1
>>> a[1][2]
6
Note also that you can use the array notation to access individual characters inside a string.
>>> var s = 'one'; >>> s[0]
"o"
>>> s[1]
"n"
>>> s[2]
"e"
There are more ways to have fun with arrays (and we'll get to that in Chapter 4), but let's stop here for now, remembering that:
- An array is a data store
- An array contains indexed elements
- Indexes start from zero and increment by one for each element in the array
- To access array elements we use the index in square brackets
- An array can contain any type of data, including other arrays
- Joomla! 1.5 Site Blueprints
- MATLAB計(jì)算機(jī)視覺(jué)經(jīng)典應(yīng)用
- Object/Oriented Programming in ColdFusion
- IBM Lotus Notes 8.5 User Guide
- 從零開(kāi)始:AutoCAD 2015中文版機(jī)械制圖基礎(chǔ)培訓(xùn)教程
- 穿越Photoshop CC
- Instant MuseScore
- 從零開(kāi)始:Photoshop工具詳解與實(shí)戰(zhàn)
- SolidWorks2016中文版從入門(mén)到精通/CAX工程應(yīng)用叢書(shū)
- Photoshop CS6中文版基礎(chǔ)與實(shí)例教程(第6版)
- Oracle Fusion Middleware Patterns
- SharePoint Designer Tutorial: Working with SharePoint Websites
- 動(dòng)畫(huà)制作基礎(chǔ)(項(xiàng)目教學(xué)版)
- 攝影師的后期必修課(RAW格式篇)
- 剪輯師寶典:視頻剪輯思維與案例實(shí)戰(zhàn)