(1)使用new关键字
var daysOfWeek = new Array()
var daysOfWeek = new Array(7)
var daysOfWeek = new Array('Sunday' , 'Monday', 'Tuesday', 'Wednesday', 'Thursday');
(2)用[]形式
var daysOfWeek = [];
var daysOfWeek = ['Sunday' , 'Monday', 'Tuesday', 'Wednesday', 'Thursday'];
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers.push(10); //就会看到从0到10的值
numbers.unshift(-2); //就会输出-2到10的值
numbers.pop(); //输出-2到9的值
numbers.shift(); //输出0到9的值
numbers.splice(5,3); //第一个参数为指定位置/索引,第二个参数为相应数量的元素,即删除了从数组索引5开始的3个元素
//就意味着numbers[5]、numbers[6]、numbers[7]从数组中删除了,数组里的值变为:0、1、2、3、4、8、9
现在,想把5、6、7插入到数组里,放到之前删除元素的位置上,可以再次使用splice
numbers.splice(5, 0, 5, 6, 7); //此时数组里的值又变为:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
连接2个或更多数组,并返回结果
var zero = 0;
var positiveNumbers = [1, 2, 3];
var negativeNumbers = [-3, -2, -1];
var numbers = negativeNumbers.concat(zero, positiveNumbers); //输出结果为:-3,-2,-1,0,1,2,3
8、every方法迭代
every方法会迭代数组中的每个元素,直到返回false。
var isEven = function(x) {
//如果x是2的倍数,就返回true
console.log(x);
return(x % 2 ==0);
}
var myNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
myNumbers.every(isEven); //数组中第一个元素是1,不是2的倍数,isEven函数返回false,every执行完毕。
与every方法类似,不过some方法会迭代数组的每个元素,直到函数返回true
myNumbers.some(isEven); //第二个被迭代的元素是2,isEven返回true,迭代结束
迭代整个数组,输出所有isEven返回true的值,与for循环的结果相同
返回新数组的遍历方法,第一个是map
var myMap = myNumbers.map(isEven); //myMap里的值是:[false, true, false, true, false, true,false, true,false, true, false, true, false, true, false]
还有一个filter,它返回的新数组由使函数返回true的元素组成,
var evenNumbers = myNumbers.filter(isEven); //evenNumbers中的值为[2, 4, 6, 8, 10, 12, 14]
var arr = ["a","bc","def","g"];
var ret = arr.reduce(function(prev, cur, index, _arr){
console.log(prev, cur);
return cur;
}); // ret = "g"
//打印如下
//a bc
//bc def
//def g
var arr = ["a","bc","def","g"];
var ret = arr.reduce(function(prev, cur, index, _arr){
console.log(prev, cur);
return cur;
},"hello"); // ret = "g"
//打印如下
//hello a
//a bc
//bc def
//def g
reduce的返回值是最后一次运算的结果, 它的回调函数中的第一个参数是上次运算的结果,第二个是当前元素的值,第三个是当前下表,第四个是数组。
var arr = ["a","bbb","c","d","a","e","c","f"];
var ret1 = arr.lastIndexOf("a"); //ret = 4
var ret2 = arr.lastIndexOf("a",2); //ret = 0
var ret3 = arr.lastIndexOf("a",-2); //ret = 4
var ret4 = arr.lastIndexOf("a",-6); //ret = 0
var ret5 = arr.lastIndexOf("a",-10); //ret = -1
lastIndexOf找不到所需的元素时,返回-1;从指定位置开始往前找元素第一次出现的位置,如果第二个参数为空的话,默认是数组的长度。
var arr = ["a","b","c","d","a","e","c","f"];
var ret1 = arr.indexOf("a"); //ret = 0
var ret2 = arr.indexOf("a",2); //ret = 4
var ret3 = arr.indexOf("a",-2); //ret = -1
var ret4 = arr.indexOf("a",-6); //ret = 4
var ret5 = arr.indexOf("a",-10); //ret=0
indexOf找不到所需的元素时,返回-1; 从指定位置开始往后找元素第一次出现的位置,如果第二个参数为空的话,默认为0;
var arr = [1,12,4];
var ret = arr.toString(); // ret = 1,12,4
var arr= ["a","b","c","d","e","f"];
var result1 = arr.slice(2);
var result2= arr.slice(1,4);
var result3= arr.slice(2,-2);
var result4= arr.slice(-3,-1);
console.log(result1 ); //["c","d","e","f"];
console.log(result2 ); //["b","c","d"];
console.log(result3 );//["c","d"]
console.log(result4 );//["d","e"]
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr1 = arr.reverse();
console.log(arr1);//10,9,8,7,6,5,4,3,2,1
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr2 = arr.sort();
console.log(arr2); //1,10,2,3,4,5,6,7,8,9 是因为把元素默认为字符串进行比较的
我们可以在sort中传入自己写的比较函数,如下:
var arr = [10,9,8,7,6,5,4,3,2,1]
function compare(a,b){
if(ab){
return 1;
}else{
return 0;
}
}
var arr1 = arr.sort(compare);
console.log(arr1); //1,2,3,4,5,6,7,8,9,10
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arr1 = arr.join('-');
console.log(arr1); //输出1-2-3-4-5-6-7-8-9-10
暂时总结这些,后续如有补充还会更新~