js Arrays

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]);   // logs 'bar'

Example 2.27: Testing the size of an array

1 var myArray = [ 'hello''world' ];
2 console.log(myArray.length);   // logs 2

Example 2.28: Changing the value of an array item

1 var myArray = [ 'hello''world' ];
2 myArray[1] = 'changed';

While it's possible to change the value of an array item as shown in “Changing the value of an array item”, it's generally not advised.

Example 2.29: Adding elements to an array (增加元素到数组)

1 var myArray = [ 'hello''world' ];
2 myArray.push('new');

Example 2.30: Working with arrays

1 var myArray = [ 'h''e''l''l''o' ];
2 var myString = myArray.join('');   // 'hello'
 
3 var mySplit = myString.split('');  // [ 'h', 'e', 'l', 'l', 'o' ]
原文地址:https://www.cnblogs.com/youxin/p/2238626.html