官术网_书友最值得收藏!

Arrays

Now that you know about the basic primitive data types in JavaScript, it's time to move to a more powerful data structure—the array.

So, what is an array? It's simply a list (a sequence) 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.

To declare a variable that contains an empty array, you use square brackets with nothing between them:

> var a = [];

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 console, you get the contents of your array:

> a;
[1, 2, 3]

Now the question is how to access the values stored in these array elements. 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:

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 the values of the 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 don't exist and return the undefined value if accessed. Check out this example:

> var a = [1, 2, 3];
> a[6] = 'new';
"new"
> a;
[1, 2, 3, undefined x 3, "new"]

Deleting elements

To delete an element, you use the delete operator. However, after the deletion, the length of the array does not change. In a sense, you get a hole in the array:

> var a = [1, 2, 3];
> delete a[1];
true
> a;
[1, undefined, 3]
> typeof a[1];
"undefined"

Arrays of arrays

Arrays can contain all types 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, Array[3]]

The Array[3] in the result is clickable in the console and it expands the array values. Let's see an example where you have an array of two elements, both of them being other arrays:

> var a = [[1, 2, 3], [4, 5, 6]];
> a;
[Array[3], Array[3]]

The first element of the array is a[0], and it's also an array:

> 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 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"
Note

Array access to strings has been supported by many browsers for a while (not older IEs), but has been officially recognized only as late as ECMAScript 5.

There are more ways to have fun with arrays (and you'll get to those in Chapter 4, Objects), 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 an element of an array, you use its index in square brackets
  • An array can contain any type of data, including other arrays
主站蜘蛛池模板: 湟中县| 荥阳市| 馆陶县| 唐河县| 射洪县| 云南省| 丰城市| 兖州市| 玉山县| 西乌珠穆沁旗| 龙陵县| 安义县| 峨边| 和田县| 永春县| 普洱| 禹州市| 海原县| 安吉县| 秦安县| 当涂县| 贡觉县| 微山县| 翁源县| 丹寨县| 五台县| 特克斯县| 老河口市| 铁岭县| 舟山市| 类乌齐县| 天峨县| 曲周县| 若尔盖县| 英吉沙县| 新源县| 汝阳县| 城市| 上虞市| 桂平市| 慈利县|