1.数组的创建
var arr1 = new Array();//创建一个空数组
var arr2 = new Array(3);//创建一个长度为3的数组;
var arr3 = new Array("小张","小黄","小明");
//创建一个数组,数组内容为:"小张","小黄","小明"
var arr4 = ["小张","小黄","小明"];
//
console.log(arr1,arr2,arr3,arr4);
2.星期几
var arr = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var d = new Date();
var D =d.getDay();//0-6
console.log("今天是",arr[D]);
3.数组遍历
var arr = ["小黄","小明","小张","小王"];
//一个一个输出数组的值;
//数组下标从0开始
//数组的长度 length
//循环要点 1:循环的初始值第0个;
// 2:循环的结束条件length-1;
// 3:循环操作,输出元素
for(var i=0;i<=arr.length-1;i++){
console.log(arr[i])
}
console.log(i);
4.数组排序
var arr = [1,5,15,199,20,35,40,60,300,5.6];
arr = arr.sort();
console.log("一般排序结果",arr);
//升序functionasc(a,b){if(a>b){return1;}else{return-1;}}//降序functiondesc(a,b){if(a>b){return-1;}else{return1;}}vararr1=arr.sort(asc);console.log("排序结果asc",arr1);vararr2=arr.sort(desc);console.log("排序结果desc",arr2);
5.数组与字符串转换
var str ="我-好-中意你-呀-靓-仔-!";
//把字符串转换为数组
var arr = str .split("-");
//把str分割为数组,分割符为-;
console.log(arr);
varstr2="我好中意你呀靓仔!";vararr2=str2.split("");console.log("字符分割后的数组",arr2);arr2.reverse();//反转数组console.log("翻转后的数组",arr2);varstr3=arr2.join("");//把数组转换为字符串,连接符为空console.log(str3,str2);
作者:闹_ff4f
链接:https://www.jianshu.com/p/d3d6c794660b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。