Arrays
Arrays are zero-indexed, ordered lists of values. They are a handy way to
store a set of related items of the same type (such as strings), though in
reality, an array can include multiple types of items, including other
arrays.
To create an array, either use the object constructor or the literal
declaration, by assigning the variable a list of values after the
declaration.
1 2 3 4 5 |
|
The literal declaration is generally preferred. See the Google
Coding Guidelines for more information.
If the values are unknown, it is also possible to declare an empty array, and
add elements either through functions or through accessing by index:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
.push()
is a function that adds an element on the end of
the array and expands the array respectively. You also can directly add items by
index. Missing indices will be filled with undefined
.
1 2 3 4 5 6 7 8 9 |
|
If the size of the array is unknown, .push()
is far
more safe. You can both access and assign values to array items with the
index.
1 2 3 4 5 |
|
Array Methods and Properties
.length
The .length
property is used to determine the amount
of items in an array.
1 2 3 4 5 |
|
You will need the .length
property for looping through
an array:
1 2 3 4 5 6 7 8 9 |
|
.concat()
Concatenate two or more arrays with .concat()
:
1 2 3 |
|
.join()
.join()
creates a string representation of an array by
joining all of its elements using a separator string. If no separator is
supplied (i.e., .join()
is called without arguments) the
array will be joined using a comma.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
.pop()
.pop()
removes the last element of an array. It is the
opposite method of .push()
:
1 2 3 4 5 6 7 8 |
|
link.reverse()
As the name suggests, the elements of the array are in reverse order after
calling this method:
1 2 |
|
.shift()
Removes the first element of an array.
With .push()
and .shift()
, you can
recreate the method of a queue:
1 2 3 4 5 6 7 8 |
|
.slice()
Extracts a part of the array and returns that part in a new array. This
method takes one parameter, which is the starting index:
1 2 3 4 5 6 7 |
|
.splice()
Removes a certain amount of elements and adds new ones at the given index. It
takes at least three parameters:
1 |
|
- Index – The starting index.
- Length – The number of elements to remove.
- Values – The values to be inserted at the index
position.
For example:
1 2 3 4 |
|
.sort()
Sorts an array. It takes one parameter, which is a comparing function. If
this function is not given, the array is sorted ascending:
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 |
|
The return value of descending (for this example) is important. If the return
value is less than zero, the index of a
is
before b
, and if it is greater than zero it‘s vice-versa. If
the return value is zero, the elements‘ index is equal.
.unshift()
Inserts an element at the first position of the array:
1 2 3 4 5 |
|
.forEach()
In modern browsers it is possible to traverse through arrays with
a .forEach()
method, where you pass a function that is
called for each element in the array.
The function takes up to three arguments:
- Element – The element itself.
- Index – The index of this element in the array.
- Array – The array itself.
All of these are optional, but you will need at least
the Element parameter in most cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Javascript Array,布布扣,bubuko.com