数组:
面向对象的方式创建:
var arr01 = new Array(1,2,3,"abc");
直接创建:
var arr02 = [1,2,3,"abc"]
alert (arr02.length);
alert(arr02[3]);
var arr03 = [[1,2,3],["a","b","c","d"],[true,false]];
alert(arr03.length);//3
alert(arr03[].length);//3
alert(arr03[1].length);//4
alert(arr03[1][0].length);//a
数组的常用方法:
var a = [1,2,3,4];
var b = a.join("-");
alert(b) //1-2-3-4
var a = [1,2,3,4];
a.push(5);
alert(a);//1,2,3,4,5
var a = [1,2,3,4];
a.pop();
alert(a);//1,2,3
a.unshift(0);#在最前边添加数字
alert(a);//0,1,2,3,4
a.shift(); #删除最前边的数字
alert(a);//2,3,4
var arr2 = ["a","b","c","a"];
var num = arr2.indexof("a"); #返回第一次出现的索引
alert(num)
var arr2 = ["a","b","c","a"];
arr2.splice(2,1); #删除索引为2的值的数字
arr2.splice(2,1,"e")#把索引为2 的值删除并插入e
数组去重:
var a= [1,2,3,4,5,6,7,8,2,1,5];
var a2 = [];
for (var i = 0;i < a.length; i ++){
if (a.indexof(a[i]) == i){ #判断是否是第一次出现
a2.push(a[1]);
}
}
字符串处理的方法:
var a =charAT(索引)
var a="Micrisoft Yahei";
var b = a.substring(10,15); //Yahei
(10)
定时器:
var timer = setTimeout(function){
alert("hello");
},3000);
- 清除定时器:
clearTimeout(timer)
setInterval --反复执行的定时器
字符串反转;
split 字符串转成数组:
var arr = str.split(' ')
isNaN 判断是不是数字
网友评论