1:Array对象用于在单个变量中存储多个值
typeof(arrArr) 'object'
var arr1 = [];//创建一个空数组
eg:数组是个引用类型
var a = [1,2,3];
var b = [1,2,3];
console.log(a == b);
//false
构造函数创建
var arr1 = new Array();//创建空数组
var arr2 = new Array(10);//创建一个长度为10的数组
new Array(-1); //Uncaught RangeError: Invalid array length(…)
new Array(undefined) //[undefined]
var arr3 = new Array(5,4,3,2,1);//创建数组并初始化
es6 Array.of 创建数组
Array.of()
//[]
Array.of(undefined)
//[undefined]
Array.of(1)
//[1]
Array.of(3)
//[3]
Array.of(1, 2)
//[1, 2]
Array.of(-1)
//[-1]
2:length 属性
var arrArr = [1,2,3,4,5];
console.log(arrArr.length); //5
数组的最后一项是length-1
console.log(arrArr[arrArr.length-1]) //5
3:数组的遍历
var arrArr = [1,2,3,4,5];
for (var i = 0; i < arrArr.length; i++) {
//arrArr[i]....
};
4:数组中常见的方法
(1)删除数组的最后一项,返回的是删除的值
pop()
eg:
var arr = ["1","2"];
var arrv=arr.pop();
console.log(arr);
console.log(arr2);
//["1"]
//2
(2) 向数组的最后添加值,返回的是新数组的长度
push()
eg:
var arr = ["1","2"];
var arrv=arr.push("3","4","5");
console.log(arr);
console.log(arrv);
//["1", "2", "3", "4", "5"]
//5
(3) 删除数组的第一项,返回的是删除的值
shift()
eg:
var arr = ["1","2"];
var arrv=arr.shift();
console.log(arr);
console.log(arrv);
//["2"]
//1
(4) 向数组的开头添加值,返回的是新数组的长度
unshift()
eg:
var arr = ["1","2"];
var arrv=arr.unshift("3","4","5");
console.log(arr);
console.log(arrv);
//["3", "4", "5", "1", "2"]
//5
(5)concat()
合并两个数组,原数组不变。concat是把arr1和arr2合并为一个新数组返回
eg:
var arr1 = ["1","2"];
var arr2 = ["3","4"];
var newarr=arr1.concat(arr2,["5","6"]);
console.log(arr1);
console.log(arr2);
console.log(newarr);
//["1", "2"]
//["3", "4"]
// ["1", "2", "3", "4", "5", "6"]
(6)slice英文意思是:切成片,切下,划分
slice()
arrayObject.slice(start,end)
从已有的数组中返回选定的元素,从下标为start的元素开始,若为负数,表示从最后开始,-1表示最后一项,
到下标为end之前的一项
eg:
var arr1 = ["1","2","3","4","5"];
var arr2 = arr1.slice(1,4);
console.log(arr1);
console.log(arr2);
//["1", "2", "3", "4", "5"]
//["2", "3", "4"]
(7)splice()
从数组中添加删除项,返回被删除的项目。
arrayObject.splice(index,howmany,item1,.....);
index规定的是添加删除项的位置,若为负数,表示从最后开始,-1表示最后一项
howmany表示的是要删除的项目的数量。为0表示的是不删除。只是添加
item1,.....向数组中添加的新项(可选)
eg:
var arr = ["A","B","C","D","E","F","G"];
var newarr=arr.splice(2,4,"1");
console.log(arr);
console.log(newarr);
//["A", "B", "1", "G"]
//["C", "D", "E", "F"] 被删除的项
eg:
var arr = ["A","B","C","D","E","F","G"];
var newarr=arr.splice(2,0,"1");
console.log(arr);
console.log(newarr);
//["A", "B", "1", "C", "D", "E", "F", "G"]
//[] 表示的是删除0项
eg:
var arr = ["A","B","C","D","E","F","G"];
var newarr=arr.splice(2,3); //只删除不添加
console.log(arr);
console.log(newarr);
//["A", "B", "F", "G"]
//["C", "D", "E"]
(8)reverse()
颠倒数组中元素的顺序(改变的是原数组)
arrayObject.reverse();
var arr = ["A","B","C","D","E","F","G"];
var arr1=arr.reverse();
console.log(arr);
console.log(arr1);
//["G", "F", "E", "D", "C", "B", "A"]
//["G", "F", "E", "D", "C", "B", "A"]
(9)sort()
对数组的元素进行排序(默认是按照字符集的格式进行排序)
sortby规定排序的顺序,必须是函数
arrayObject.sort(sortby);
var arr = [1,3,2,4,6,9];
arr.sort();
console.log(arr);
//[1, 2, 3, 4, 6, 9]
sortby规定排序的顺序,只能是个函数。两参数的差值小于0,则表示a必须出现在b前面,否则在b后面
若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
若 a 等于 b,则返回 0。
若 a 大于 b,则返回一个大于 0 的值。
var arr = [2,5,4,23,2,45];
arr.sort(function(a,b){
if(a < b){
return -1; //a-b返回一个小于0.a在b的前面。a比b小。升序排列
}else if(a > b){
return 1; //a-b返回一个大于0.说明a比b大。升序排列。a在b的后面
}else if(a == b){
return 0; //a-b返回一个等于0.说明a等于b。
}
});
console.log(arr);
//[2, 2, 4, 5, 23, 45]
(10)join()
把数组中的所有元素放入一个字符串中
separator为分隔符
arrayObject.join(separator)
eg:
var arr = [1,2,3,4,5,6,7];
var str = arr.join(",");
console.log(str);
//1,2,3,4,5,6,7