(2)数组
□ []
□ 一组数据的集合
□ 数据中的所有数组元素,使用逗号隔开,每一个数组元素数据类型任意
□ 数组长度可以动态修改
□ 1、创建
® a、数组字面量
◊ var arr = []; //空数组
◊ var arr = [null, undefined, "", false, {name: "lisi"}]
® b、构造函数
◊ var arr = new Array(); //空数组
◊ var arr = new Array("hello", false); //创建非空数组
◊ var arr = new Array(10); //创建一个数组长度为10的数组
◊ var arr = new Array(10, 20);
如果参数为1个参数,并且参数类型为number类型,相当于创建一个以number为长度的数组
如果两个以上参数,相当于将参数作为数组元素创建数组
□ 2、访问数组元素
每一个数组元素都对应一个数组下标(属性名)
数组下标从0开始(number)
arr[index]:访问+设置
var arr = [1, 2,3];
console.log(arr[0]);
□ 3、检测数组
function:
typeof function:function
arr:
typeof arr:object
isArray():构造函数(静态方法)
Array.isArray(arr);
当前arr是否为Array类型
□ 4、转换数组
toString()
arr.toString(); 重写了toString()
join('-')
将数组转换为string,用'-'分隔每一个数组元素
□ 5、方法
® 添加/删除数组元素
◊ 栈方法:
} 先入后出
pop():
删除数组元素
参数:不需要
返回值:删除的数组元素
push():
添加数组元素
参数:添加的数组项
返回值:添加后的数组长度
◊ 队列方法:
} 先入先出
unshift():
添加数组元素
参数:添加的数组项
返回值:添加后的数组长度
shift():
删除数组元素
参数:不需要
返回值:删除的数组元素
® 排序
◊ reverse:
数组反转
参数:不需要
返回值:反转之后的数组,并且修改原数组
◊ sort():
数组排序
参数:
0:
每一个数组项,相当于会调用toString()之后进行比较,按照字符编码顺序进行比较
var arr = [1, 5, 3, 0, 11, 6];
var result = arr.sort();
console.log(result); //Array(6) [ 0, 1, 11, 3, 5, 6 ]
console.log(arr);
1:
回调函数
var result = arr.sort(function(a, b){
// console.log(a, b);
if(a > b){
return 1; //将a放在b之后
}
else if(a < b){
return -1; //将a放在b之前
}else {
return 0; //a=b不排序
}
});
返回值:排序后的数组(在原数组基础上进行排序)
var arr = [{
name: 'a',
age: 20,
score: 5
},{
name: 'b',
age: 20,
score: 1
},{
name: 'a',
age: 20,
score: 2
}]
® 操作方法(非静态方法)
◊ concat:
数组拼接
(arr, arr1)
◊ slice():不修改原数组
数组切割/截取
参数:
0:
返回值:切割后的新数组默认从数组开头开始切割,切割到数组末尾,组成新数组
1:
参数:index 开始切割的位置
返回值:从当前位置开始切割,到数组末尾的新数组
2:
begin end
返回值:从begin位置开始切割,切割到end位置结束,不包含结束位置
◊ splice():
数组切割
参数:
0:
返回值:空数组
1:
index:begin
返回值:从begin位置开始切割,到数组末尾结束,修改原数组
2:
begin number
返回值:从begin位置开始切割,切割number个数组元素所组成的新数组
number > 剩余数组元素个数——》剩余数组元素
3:
begin number "content"
返回值:
替换:从begin位置开始,删除number个数组元素,在begin位置插入content
插入:begin 0 'content'
从begin位置开始,删除0个,在begin位置插入content
◊ indexOf(key, index):
查找数组元素
参数:content index
通过===进行查找
返回值:
找到了返回当前数组元素的下标
找不到返回-1
默认从前向后查找,找到一个满足条件的元素返回
◊ lastIndexOf(key, index):
查找数组元素
返回值:
查找数组元素
默认从后向前查找
到不到返回-1
找到返回当前数组元素的下标
® 迭代方法
◊ every():
判断数组中元素是否每一个都满足条件,都满足条件返回true,只要有一个不满足,返回false
var result = arr1.every(function(item, index, arr){
return item > 4;
});
console.log(result);
◊ some():
判断数组中是否含有满足条件的数组元素,存在一个即返回true,否则返回false
◊ map():不修改原数组
对每一个数组元素进行操作,返回值操作后的数组
var result = arr1.map(function(item, index, arr){
return item + 10;
});
console.log(result);
console.log(arr1);
◊ filter():
数组过滤
将满足条件的数组元素筛选出来放在一个新数组中
◊ forEach()
for(){}
循环体
index item
arr.forEach(function(item, index, arr){
console.log(item, index);
});
参数:
回调函数 this
回调函数:
有几个数组元素,回调函数被执行几次
参数:item index arr
数组元素 下标 arr
item:每次循环所获取的数组元素
index:每次循环的数组元素所对应的数组下标
arr:当前所循环的数组
this:
当前回调函数中this的指向
◊ myForEach():
} 声明位置
} 判断参数个数以及类型
} 如何循环执行回调函数
} 回调函数中的this值指向
var arr = [1, 2, 3, 4];
Array.prototype.myForEach = function(){
if(arguments.length == 1 && typeof arguments[0] == 'function'){
for(var i = 0; i < this.length; i++){
arguments[0].call(window, this[i], i,this);
}
}else if(arguments.length == 2){
// arguments[0]:handler
// arguments[1]:this
for(var i = 0;i < this.length; i++){
arguments[0].call(arguments[1], this[i], i, this);
}
}
}
arr.myForEach(function (item, index, arr) {
console.log(item, index, arr);
// console.log(this);
},{});
◊ reduce():
回调函数 this
回调函数
pre item index arr
pre:prevent,上一次回调函数执行的返回值
如果第一次执行时,并且没有设置第二个参数时,pre为数组第一个元素;如果设置了第二个参数时,pre为第二个参数
this:
回调函数中的this