Arrays
Arrays are zero-indexed 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.
Example 2.25: A simple array
1 |
var myArray = [ 'hello' , 'world' ]; |
Example 2.26: Accessing array items by index
1 |
var myArray = [ 'hello' , 'world' , 'foo' , 'bar' ]; |
2 |
console.log(myArray[3]); |
Example 2.27: Testing the size of an array
1 |
var myArray = [ 'hello' , 'world' ]; |
2 |
console.log(myArray.length); |
Example 2.28: Changing the value of an array item
1 |
var myArray = [ 'hello' , 'world' ]; |
2 |
myArray[1] = 'changed' ; |
Example 2.29: Adding elements to an array (增加元素到数组)
1 |
var myArray = [ 'hello' , 'world' ]; |
Example 2.30: Working with arrays
1 |
var myArray = [ 'h' , 'e' , 'l' , 'l' , 'o' ]; |
2 |
var myString = myArray.join( '' ); |
3 |
var mySplit = myString.split( '' ); |