一、创建数组对象有三种方法:
1.新建一个长度为零的数组
var 变量名=new Array( );
var x = new Array();
console.log(x); //[]
2.新建一个长度为N的数组
var 变量名=new Array( n );
var y = new Array(5);
console.log(y); //长度为5的数组
3.新建一个指定长度的数组,并赋值
var 变量名=new Array( 元素1,元素2,元素3,…);
var z = [1,"大宝剑",true,4.6879,new Date()];
console.log(z); //数字,字符串,布尔,浮点,时间
二、访问数组
数组中的序列号:
在JavaScript数组中的元素序列号是从0开始计算的,然后依次加1 。
引用数组元素 :通过数组的序列号可以引用数组元素。
为数组元素赋值或取值,其语法规则是:
为数组元素赋值:数组变量[i]=值;
使用数组元素为变量赋值:变量名=数组变量[i];
1.数组的索引访问 (0,length-1)
var x = ['a','b','c','d']
console.log(x[1]) //a
console.log(x[10]) //undefined
console.log(x.length) //4
2.数组的索引修改,可以修改!!!
var x = ['a','b','c','d']
console.log(x) //["a", "b", "c", "d"]
x[0]='大宝剑';
console.log(x) //["大宝剑", "b", "c", "d"]
三、遍历数组(即逐个访问数组元素对象)
1.for循环
var x = [3,2,4,8,9,2,12];
for(var i=0;i<x.length;i++){
console.log(x[i])
}
2.for-in遍历获得的是键
var x = [3,2,4,8,9,2,1];
for(var i of x){
console.log(i)
}
3.for-of遍历获得的是值
var x = [3,2,4,8,9,2,1];
for(var i of x){
console.log(i)
}
4.forEach遍历数组
var x = [3, 2, 4, 8, 9, 2, 12];
var y = x.forEach(function(value, index, item) {
console.log(value,index,item) //数组中每个值,数组中对应的下标,数组本身
})
5.map遍历映射,返回新处理的数组
var x = [1, 2, 3, 4];
var y = x.map(function(value, index, item) {
console.log(value, index, item); //数组中每个值,数组中对应的下标,数组本身
return value * value;
});
console.log(y); // [1, 4, 6, 19]
6.filter遍历过滤,返回新的数组
var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var y = x.filter(function(value, index, item) {
return value % 2 == 0;
})
console.log(y)
四、数组的方法
1.增
(1)push末尾增加
var names = ['张三','李四','王五','赵六','白七'];
names.push('一');
console.log(names);
(2)unshift开头添加
var names = ['张三','李四','王五','赵六','白七'];
names.unshift('二');
console.log(names);
(3)splice指定位置添加
var names = ['张三','李四','王五','赵六','白七'];
names.splice(3,0,'三'); //添加或删除的元素的位置,要删除的元素个数、为0不删除任何元素
console.log(names); // ["张三", "李四", "王五", "三", "赵六", "白七"]
2.删
(1)shift 开头删除
var names = ['张三','李四','王五','赵六','白七'];
names.shift();
console.log(names); //["李四", "王五", "赵六", "白七"]
(2)pop末尾删除
var names = ['张三','李四','王五','赵六','白七'];
names.pop();
console.log(names); //["张三", "李四", "王五", "赵六"]
(3)splice指定位置删除
var names = ['张三','李四','王五','赵六','白七'];
names.splice(2,1);
console.log(names); //["李四", "王五", "赵六", "白七"]
3.改
var names = ['张三','李四','王五','赵六','白七'];
names[2] = '大宝剑';
console.log(names); //["张三", "李四", "大宝剑", "赵六", "白七"]
4.查
var names = ['张三','李四','王五','赵六','白七'];
console.log(names.indexOf('大宝剑')); // 返回查找的索引,找不到返回-1
console.log(names.indexOf('张三')); // 返回查找的索引,找到返回索引值
五、数组的排序(sort)
var x = [17,5,7,2,2,63,8,4];
x.sort(function(a,b){
return a>b;
})
console.log(x); //[2, 2, 4, 5, 7, 8, 17, 63]
六、反序
var x = [17,5,7,2,2,63,8,4];
x.reverse()
console.log(x) //[4, 8, 63, 2, 2, 7, 5, 17]
七、连接合并
1.concat用于连接两个或多个数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
var x = [1,2,3]
var y = ['a','b','c']
var z = x.concat(y);
console.log(z) // [1, 2, 3, "a", "b", "c"]
2.join该字符串是通过把数组的每个元素转换为字符串,然后把这些字符串连接起来,在两个元素之间插入join括号内字符串而生成的,如果省略括号内的参数,则是用逗号作为分隔符。
var x = [1,2,3];
console.log(x.join('')) //123
3.apply
var x = [1,2,3]
var y = ['a','b','c']
x.push.apply(x,y); //123abc
4.push
var x = [1,2,3]
var y = ['a','b','c']
x.push(...y); //123abc
八、slice切片数组
var x= 'abcdehgisdldasff'.split("") //分割
console.log(x)
console.log(x.slice(2,5))