网易微专业-JS10数组

1、创建数组
var array = new Array();
var array = []; 常用
var array = [1,6,3];

2、arr.length 数组里面有多少元素

3、获取数组元素:用索引

Paste_Image.png

修改数组元素: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()   将数组内元素顺序倒过来
![ ](http://upload-images.jianshu.io/upload_images/316258-6852c4a48abec97e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

7、arr.sort([compareFunction])  排序,直接改变原有数组
注: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()  获取数组第一个元素,同时原数组删除第一个元素

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/316258-55de1d95f32beecd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

11、arr.pop()  取出数组最后一个元素,同时原数组删除最后一个元素

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])  从数组中获取元素,不改变原数组
注:包含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)  在原数组基础上,获取一个新数组
![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:对一个数组(每项都是数值)求和,有哪些方法?
A:
>var arr = [1,2,3,4,6,7,8,9];
var sum = 0;
1、for循环
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);

你可能感兴趣的:(网易微专业-JS10数组)