創(chuàng)建數(shù)組?
創(chuàng)建一個(gè)空數(shù)組
數(shù)組在創(chuàng)建時(shí)可以不包含任何元素?cái)?shù)據(jù),即空數(shù)組。創(chuàng)建后返回一個(gè)數(shù)組對象,使用該對象可以往數(shù)組中添加元素。
var Obj = new Array();
上述語句將創(chuàng)建一個(gè)空數(shù)組。變量Obj引用創(chuàng)建后的數(shù)組對象,通過此變量可以操作數(shù)組,Array()為數(shù)組對象的構(gòu)造函數(shù)。
通過指定數(shù)組長度創(chuàng)建數(shù)組
在創(chuàng)建數(shù)組時(shí)可以指定數(shù)組的元素長度,通過這種方式可以創(chuàng)建一個(gè)有指定元素個(gè)數(shù)的數(shù)組對象 。
var Obj = new Array( Size );
Size指明新建的數(shù)組有多少個(gè)元素。數(shù)組對象的length將被設(shè)置為Size,僅指定長度但沒有實(shí)際填充元素及其數(shù)據(jù)的數(shù)組將得不到數(shù)據(jù)存儲空間
通過指定數(shù)組元素創(chuàng)建數(shù)組
新建的數(shù)組將包含創(chuàng)建時(shí)指定的元素,通常用在數(shù)據(jù)已經(jīng)準(zhǔn)備就緒的場合。
var Obj = new Array( 元素1, 元素2, …, 元素N );
在js中 與java不同的是 數(shù)組可以自動(dòng)擴(kuò)容
var students = new Array( "Peter", "Tom", "Vicky", "Jet" ); // 通過指定元素創(chuàng)建數(shù)組
? ?? for( n in students ) // 逐個(gè)輸出數(shù)組中的名字
? ?? {
? ? ? ? document.write( students[n] + " " ); // 將名字寫入當(dāng)前文檔流中
? ?? }
直接創(chuàng)建數(shù)組
JavaScript創(chuàng)建數(shù)組的另一種簡便的方式是使用“[]”運(yùn)算符直接創(chuàng)建,數(shù)組的元素也是創(chuàng)建時(shí)被指定。
var Obj = [ 元素1, 元素2, 元素3, …, 元素N ];
創(chuàng)建數(shù)組
var? p1 = ['tom', 28, 'NewYork'];
alert(p1);
讀取數(shù)組元素
var products = new Array( “洗衣粉”, “香皂”, “洗潔精” ); // 商品列表
var product = products[ 1 ]; // 取出第二種商品
直接賦值
tmpArray[0] = “value1”;
tmpArray[1] = “value2”;
通過for循環(huán)賦值
for(var i=0;i<tmpArray.length;i++){
tmpArray[i] = “2000”+i;
}
數(shù)組的主要屬性length
返回?cái)?shù)組長度的整數(shù)值
數(shù)組對象的length(長度)屬性指示了數(shù)組元素的個(gè)數(shù)。
通過設(shè)定length屬性可以指定數(shù)組的長度。
var Obj = new Array( 1, 2, 3 );
var count = Obj.length;
盡管指定了數(shù)組的length屬性,真正的有效元素只包含已經(jīng)存入數(shù)據(jù)的元素,其它沒有真正填充數(shù)據(jù)的元素仍然為空。
取值
直接使用
document.write(tmpArray[0]);
通過for循環(huán)取值
同賦值的for循環(huán)
或者 for…in循環(huán)
for(var i in book){
? ? ? document.write("book[" + i + "] "+ book[i]? + "<br>");
}
Array對象的常用方法
concat
返回一個(gè)新數(shù)組;由兩個(gè)或者更多數(shù)組組合而成
var newArray = tmpArray.concat(tmpArray)
join
返回字符串;由數(shù)組中的所有元素連接到一起,元素間的間隔符由參數(shù)指定,省略參數(shù)則用逗號分隔
var newString = tmpArray.join(“.”)
reverse
返回一個(gè)新數(shù)組;由原來的數(shù)組反轉(zhuǎn)而成
var newArray = tmpArray.reverse();
pop
移除數(shù)組中的最后一個(gè)元素并返回該元素
var newString = tmpArray.pop()
push
給數(shù)組中增加新元素,并返回?cái)?shù)組的新長度
var newLength = tmpArray.push(“a”,”b”)
shift
移除數(shù)組中的第一個(gè)元素并返回該元素
var newString = tmpArray.shift()
slice
返回一個(gè)新數(shù)組,為原數(shù)組中的一段
var newArray = tmpArray.slice(1,3)
第一個(gè)參數(shù)一般是開始坐標(biāo),第二個(gè)元素可能是結(jié)束坐標(biāo) 也可能是長度
如果是結(jié)束坐標(biāo) 參數(shù)個(gè)數(shù)=結(jié)束坐標(biāo)-開始坐標(biāo)》》第二個(gè)元素-第一個(gè)元素
如果是長度 參數(shù)個(gè)數(shù)=長度-----》》第二個(gè)元素
sort——ArraySort.html
返回一個(gè)排序后的新數(shù)組
var newArray = tmpArray.sort(AscSort)
toString
返回將Array中的元素轉(zhuǎn)為由逗號分隔的字符串
var newString = tmpArray.toString()
冒泡排序
var arry1=[23,33,17,38,3,9]
function sorta(a,b){
return a-b
}
console.log(array1.sort(sorta))
//a-b 升序 b-a降序
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var d1=new Date();//創(chuàng)建日期類型
console.log(d1);
//日期轉(zhuǎn)換
//2020/04/17? 使用構(gòu)造器 設(shè)置值
var d2=new Date("2020/4/17")
console.log(d2);
var d3=new Date(2020,4,12,14,32,15,199);
console.log(d3)
console.log(Date.UTC(2020,4,12))//距離1970年1月1日毫秒值
console.log(Date.UTC(1970,1,1,0,0,0))//距離1970年1月1日毫秒值
// 字符串解析----》》日期
console.log(Date.parse("Aug 9,1995"))
console.log(Date.parse("8/9/1995"))
//毫秒-----》》日期
var d4=new Date(Date.UTC(1970,1,1,0,0,0));//Sun Feb 01 1970 08:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
console.log(d4);
var d5=new Date(807897600000);
console.log(d5);//Wed Aug 09 1995 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
var d6=new Date();
console.log(d6.getFullYear())
console.log(d6.getMonth())
console.log(d6.getDate())
console.log(d6.getDay())
d6.setFullYear(2021);
console.log(d6)
d6.setMonth(4)//0----1月
console.log(d6)
// Date --->>轉(zhuǎn)為字符串 string
console.log(d6.toDateString())
// ceil? fioor round random
console.log(Math.ceil(10.5))
console.log(Math.ceil(-10.5))
console.log(Math.floor(10.5))
console.log(Math.floor(-10.5))
console.log(Math.round(10.5))
console.log(Math.round(10.4))
console.log(Math.random())
//范圍1~10 random
// 思路 [0,1)*10 ----->>[0,10)+1
console.log(Math.floor(Math.random()*10+1));
//字符串類型
var s1="asdf asdf asdf"
var s2=new String("asdf asdf asdf")
console.log(s1);
console.log(s2.toString());
console.log(typeof(s1));
console.log(typeof(s2.toString()));
console.log(s1.length)
//正則表達(dá)式
var reg=new RegExp('asdf','i')
var reg2=/asdf/i;
console.log(reg.test(s1));
console.log(reg2.test(s1));
console.log(reg.exec(s1));//返回?cái)?shù)組
//正則表達(dá)式必知必會
var s2="this is a google! Google";
var reg3=new RegExp("(g)oogle","ig");
console.log(reg3.test(s2))
console.log(RegExp.leftContext);
console.log(RegExp.rightContext);
console.log(RegExp.lastMatch);
console.log(RegExp.lastParen);
console.log("aaa:"+RegExp.multiline);
var reg4 =new RegExp("google");
console.log(s2.replace(reg4,"*"))
//重點(diǎn) 前端考試
function aaa(){
alert("你好")
}
//setTimeout(aaa(), 5000); //延遲5秒后彈出
//aaa();
//間隔函數(shù)
setInterval("aaa()",5000);
</script>
</head>
<body>
</body>
</html>