1瑟蜈、創(chuàng)建數(shù)組
var array = new Array();
var array = []; 常用
var array = [1,6,3];
2右冻、arr.length 數(shù)組里面有多少元素
3、獲取數(shù)組元素:用索引
修改數(shù)組元素:student[1].score = 60;
4改衩、arr.indexOf()
var tel=[110,112,114,120];
tel.indexOf(120);// 3
tel.indexOf(123);// -1```
5、arr.forEach(callback[,thisArg])
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-17b2c7a50bf5288b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6、arr.reverse() 將數(shù)組內元素順序倒過來
![ ](http://upload-images.jianshu.io/upload_images/316258-6852c4a48abec97e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7藕筋、arr.sort([compareFunction]) 排序,直接改變原有數(shù)組
注:c=b.score-a.score ,c>0,b a; c<0,a b; c=0,保持原有順序。也即倒序
![降序](http://upload-images.jianshu.io/upload_images/316258-2b0556739cf49bc0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![arr.sort() 則按照字符串Unicode碼點
注:字母順序梳码,第一個字母比完之后隐圾,開始第二個字母](http://upload-images.jianshu.io/upload_images/316258-84703632c63b3080.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
8、arr.push(element1,element2...) 往后添加一個元素
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-ada275fa2d6d5246.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
9掰茶、arr.unshift() 往前添加元素
10暇藏、arr.shift() 獲取數(shù)組第一個元素,同時原數(shù)組刪除第一個元素
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-55de1d95f32beecd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
11濒蒋、arr.pop() 取出數(shù)組最后一個元素盐碱,同時原數(shù)組刪除最后一個元素
12、arr.splice(index,howMany[,ele1[,...[,eleN]]]) 從第幾個索引開始啊胶,刪除幾個元素甸各,添加元素
功能:①替換;②刪除焰坪;③添加
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-46f0a884fc6977a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
13趣倾、arr.slice(begin[,end]) 從數(shù)組中獲取元素,不改變原數(shù)組
注:包含begin,不包含end
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-e5996e6fe0afe3bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
14某饰、arr.concat(value1,value2...valueN)
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-334b5ec6125f0ebb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
15儒恋、arr.join([separator])
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-10a5d870af378ebf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
16、arr.map(callback) 在原數(shù)組基礎上黔漂,獲取一個新數(shù)組
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-d6caeca58e12b492.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![更簡單的方法](http://upload-images.jianshu.io/upload_images/316258-0a2316a400d73e81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
17诫尽、arr.reduce(callback[,initialValue]) 求和
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-f54e79558e664db7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-380b3a03b813a718.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Q:對一個數(shù)組(每項都是數(shù)值)求和,有哪些方法炬守?
A:
>var arr = [1,2,3,4,6,7,8,9];
var sum = 0;
1牧嫉、for循環(huán)
for(var i = 0;i < arr.length;i++){
sum += arr[i];}
2、forEach
arr.forEach(function(item,index,array){
return sum += item;});
3减途、map
array.map(function(item,index,array){
return sum += item;});
4.reduce
var sum = function(preValue,item,index,array){
return preValue + item;
}
arr.reduce(sum,0);