第203天:js---Array对象常用方法

1、shift:删除原数组的第一项,返回删除元素的值;如果数组为空则返回undefined

1 //shift:删除原数组的第一项,返回删除元素的值;如果数组为空则返回undefined
2     var arr = [1, 2, 3, 4, 5];
3     var out = arr.shift();
4     console.log(arr); //[2,3,4,5]
5     console.log(out); //1
6     var arr = [];
7     var out = arr.shift();
8     console.log(arr); //[]
9     console.log(out); //undefined

2、unshift:将参数添加到原数组开头,返回数组的长度(ie6下返回undefined)

1 //unshift:将参数添加到原数组开头,返回数组的长度(ie6下返回undefined)
2     var arr = [1, 2];
3     var out = arr.unshift(-1, 0);
4     console.log(arr); //[-1,0,1,2]
5     console.log(out); //4

3、pop:删除原数组的最后一项,返回数组删除的值;如果数组为空则返回undefined

1 //pop:删除原数组的最后一项,返回数组删除的值;如果数组为空则返回undefined
2     var arr = [1, 2, 3, 4, 5];
3     var out = arr.pop();
4     console.log(arr); //[1,2,3,4]
5     console.log(out); //5
6     var arr = [];
7     var out = arr.pop();
8     console.log(arr); //[]
9     console.log(out); //undefined

4、push:将参数添加到原数组的末尾,返回数组的长度

1 //push:将参数添加到原数组的末尾,返回数组的长度
2     var arr = [1, 2, 3];
3     var out = arr.push(4, 5, 6);
4     console.log(arr); //[1,2,3,4,5,6]
5     console.log(out); //6

5、concat:返回一个将参数添加到原数组中构成的新数组

1 //concat:返回一个将参数添加到原数组中构成的新数组
2     var arr = [1, 2, 3];
3     var out = arr.concat(4, 5);
4     console.log(arr); //[1,2,3]
5     console.log(out); //[1,2,3,4,5]

6、splice(start,deleteCount,val1,val2,...):从start位置开始删除原数组deleteCount项,并从该位置起插入val1,val2,...,返回删除的项组成的新数组

 1 //splice(start,deleteCount,val1,val2,...):从start位置开始删除原数组deleteCount项,并从该位置起插入val1,val2,...,返回删除的项组成的新数组
 2     var arr = [1, 2, 3, 4, 5];
 3     var out = arr.splice(2, 2, 7, 8, 9, 10);
 4     console.log(arr); //[1,2,7,8,9,10,5]
 5     console.log(out); //[3,4]
 6     //在清空数组时仅需传递start,如果不删除所有元素,再传递deleteCount
 7     var arr = [1, 2, 3, 4, 5];
 8     var out = arr.splice(2);
 9     console.log(arr); //[1,2]
10     console.log(out); //[3,4,5]
11     //同shift
12     var arr = [1, 2, 3, 4, 5];
13     var out = arr.splice(0, 1);
14     console.log(arr); //[2,3,4,5]
15     console.log(out); //[1]
16     //同unshift
17     var arr = [1, 2, 3, 4, 5];
18     var out = arr.splice(0, 0, 22, -1, 0);
19     console.log(arr); //[22,-1,0,1,2,3,4,5]
20     console.log(out); //[]
21     //同pop
22     var arr = [1, 2, 3, 4, 5];
23     var out = arr.splice(arr.length - 1, 1);
24     console.log(arr); //[1,2,3,4]
25     console.log(out); //[5]
26     //同push
27     var arr = [1, 2, 3, 4, 5];
28     var out = arr.splice(arr.length, 0, 6, 7, 8);
29     console.log(arr); //[1,2,3,4,5,6,7,8]
30     console.log(out); //[]

7、reverse:将原数组倒序

 1 //reverse:将原数组倒序
 2     var arr = ['a', 1, {}, [2, 3]];
 3     console.log(arr); //['a', 1, {}, [2, 3]]
 4     var out = arr.reverse();
 5     console.log(arr); //[[2,3],{},1,'a']
 6     console.log(out); //[[2,3],{},1,'a']
 7     var arr = [0, 1, 5, 10, 15];
 8     arr.reverse();
 9     console.log(arr); //[15,10,5,1,0]
10     var arr = [1, 0, 5, 15, 10];
11     arr.reverse();
12     console.log(arr); //[10,15,5,0,1]

8、sort(orderfunction):sort()为升序排列

 1 //sort(orderfunction):sort()为升序排列,但是先调用每个数组项的toString()方法,然后比较字符串来排序,是按ASCII进行比较的;
 2     //所以跟reverse不同原理不同。sort()不带参数的时候是从字面上对数组排序
 3     var arr = [0, 1, 5, 10, 15];
 4     arr.sort();
 5     console.log(arr); //0,1,10,15,5 ,注意这里是字符串按ASCII进行比较的
 6     //调用这样的函数就按数值方式排列了
 7     function sortNumber(a, b) {
 8         return a - b
 9     }
10     arr.sort(sortNumber);
11     console.log(arr);   //0,1,5,10,15

9、slice(start,end):返回从原数组中指定startend(不包含该元素)之间的项组成的新数组

1 //slice(start,end):返回从原数组中指定start到end(不包含该元素)之间的项组成的新数组,如只有一个参数,则从start到数组末尾
2     var arr = [1, 2, 3, 4, 5, 6, 7, 8];
3     var out = arr.slice(2,3);
4     console.log(arr); //[1, 2, 3, 4, 5, 6, 7, 8]
5     console.log(out); //[3]

10、join(separator):将数组的元素组成一个字符串

1 //join(separator):将数组的元素组成一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符
2     var arr = [1, 2, 3, 4, 5, 6];
3     var out = arr.join();
4     console.log(arr); //[1, 2, 3, 4, 5, 6]
5     console.log(out); //1,2,3,4,5,6
6     out = arr.join("");
7     console.log(out); //123456
8     out = arr.join("&");
9     console.log(out); //1&2&3&4&5&6

 

posted @ 2018-03-24 22:42  半指温柔乐  阅读(182)  评论(0编辑  收藏  举报