赞
踩
1. concat() 方法用于连接两个或多个数组
- let a =[1,2,3], b=[4,5,6];
- let c = a.concat(b);
- console.log(a);//[1,2,3]
- console.log(b);//[4,5,6]
- console.log(c);// [1,2,3,4,5,6]
2. join(); 用于把数组中的所有元素放入一个字符串,元素通过制定分隔符进行分隔的
- let a = [1,2,3];
- let aa = a.join(':');
- console.log(a);// [1,2,3]
- console.log(aa);// '1:2:3',如果join 什么也不传默认用, 分隔
3. slice();
数组的截取(start, end), start: 开始索引, end: 结束索引,不写默认截取到末尾
start, end 都不写: 默认截取全部
- let a = [1,2,3];
- let a1 = aa.slice();
- let a2 = aa.slice(1);
- let a3 = aa.slice(1,2);
- console.log(a);//[1,2,3]
- console.log(a1);//[1,2,3]
- console.log(a2);//[2,3]
- console.log(a3);//[2,3]
4. toString(); 把数组转换成字符串
- let a = [1,2,3];
- let aa = a.toString();
- console.log(a);//[1,2,3]
- console.log(aa);//'1,2,3'
5. filter(); 返回为真的,放到一个新的数组里
- let a = [1,2,3];
- let aa = a.filter(item =>{return item>1});
- console.log(a);//[1,2,3]
- console.log(aa); // [2,3]
6. map(); 在现有数据基础上进行更改,返回一个新的数组
- let a = [1,2,3];
- let b = a.map(item =>{ return item * 2});
- console.log(a);//[1,2,3]
- console.log(b);//[2,4,6]
7. find(); 返回为真的那一项, 找到真则返回不再继续找
- let a = [1,2,3];
- let b = a.find(item => item===2);
- console.log(a);//[1,2,3]
- console.log(b);//[2]
1. pop(); 用于删除并返回 数组最后一个元素
- let a = [1,2,3];
- let b = a.pop();
- console.log(a);//[1,2]
- console.log(b);//[3]
2. push(); 向数组末尾添加一个或多个元素,并返回新的长度
- let a = [1,2,3];
- let b = a.push(7);
- console.log(a);//[1,2,3,7]
- console.log(b);//4
3. shift(); 删除并返回第一个元素的值
- let a = [1 ,2 ,3];
- let b = a.shift();
- console.log(a)// [2,3]
- console.log(b);//1
4. unshift(); 向数组的开头添加一个或更多元素,并返回新的长度
- let a = [1,2,3];
- let b = a.unshift(7);
- console.log(a);// [7,1,2,3]
- console.log(b); // 4
5. reverse(); 颠倒数组元素中的顺序
- let a = [1,2,3];
- a.reverse();
- console.log(a);// [3,2,1]
6. sort(); 排序
- let a = [1,2,5,4,3];
- a.sort();
- console.log(a);// [1,2,3,4,5] 默认从小到大
-
- // 想要从大到小排序
- let b = [1,2,5,4,3];
- b.sort((a,b)=> b-a);
- console.log(b); // [5,4,3,2,1]
7. splice(start, length); 截取数组中的一部分,start 开始索引,length截取的长度,返回截取的内容
- let a = [1,2,3,4,5];
- let a1 = a.splice();
-
- let b =[1,2,3];
- let b1 = b.splice(1,1);
- console.log(a);//[1,2,3,4,5]
- console.log(a1);// []
- console.log(b);//[1,3]
- console.log(b1);// [2]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。