JavaScript复习之内置对象,JSON
内置对象
String对象
基础介绍
length:长度
indexOf():指定字符在字符串中第一次出现的位置
lastIndexOf():指定字符在字符串中最后一次出现的位置
substring():截取字符串
一个参数:截取到末尾
两个参数:含头不含尾
substr(m,n):截取字符串
两个参数:从索引为m开始,截取n个
split():拆分字符串
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
var content = 'We are family are we!!!';
console.log(content.length);
console.log(content.indexOf('a'));
console.log(content.lastIndexOf('a'));
console.log(content.substring(3));
console.log(content.substring(4, 8));
console.log(content.substr(4, 8));
console.log(content.toUpperCase());
console.log(content.toLowerCase());
var str = 'id:1,name:tom,age:18';
console.log(str.split(',')[1].split(':')[1]);
</script>
</html>
Math对象
基础介绍
ceil():向上取整
floor():向下取整
abs():绝对值
round():四舍五入
random():随机数 [0,1) *** 扩大倍数、移动区间
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
var myNum=13.14;
console.log(Math.ceil(myNum));
console.log(Math.floor(myNum));
console.log(Math.abs(myNum));
console.log(Math.round(myNum));
console.log(Math.random());
console.log(parseInt(Math.random()*4+1));
</script>
</html>
Array对象
基础介绍
push():末尾追加
pop():末尾删除
unshift:头部添加
shift():头部删除
splice():删除 添加
splice(m,n,p):从索引为m位置,删除n个内容,添加内容p
splice(m,n):从索引为m位置,删除n个内容
join():
拼接成字符串
没有参数:默认使用逗号拼接
传入参数:使用指定内容拼接
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
var myArr = [10, 2, 30, 4, 15];
console.log(myArr.join());
console.log(myArr.join('-'));
</script>
</html>
Date对象
基础介绍
getFullYear():年份值 4位整数
getMonth():月份 0~11
getDate():日 1~31
getHours():小时 0~59
getMinutes():分钟 0~59
getSeconds():秒 0~59
getDay():星期 0~6
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
var nowDate = new Date();
console.log(nowDate);
console.log(nowDate.getFullYear());
console.log(nowDate.getMonth());
console.log(nowDate.getDate());
console.log(nowDate.getHours());
console.log(nowDate.getMinutes());
console.log(nowDate.getSeconds());
console.log(nowDate.getDay());
</script>
</html>
JSON
基础介绍
(1)JavaScript Object Notation
(2)JS的对象式的表现形式
(3)轻量级的数据交换格式
后端:符合JSON格式的字符串
前端:转换JSON对象
(4)jsoncn->检测内容是否合法
(5)标准写法:
{"key":val,"key":val,"key":val,...}
val:数字|布尔类型->不加引号 字符串->加引号
(6)简化写法:
{key:val,key:val,key:val,...}
val:数字|布尔类型->不加引号 字符串->加引号
(7)获取值:
.key=val
(8)常见搭配:
数组和JSON嵌套:
数组:索引
JSON:.key=val
(9)遍历数据:****
要遍历的数据.forEach(function(data,idx){})
data:遍历到的每一项数据 idx:索引值
(10)parse():
将字符串格式的值转换为JSON对象
案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
var stu = {
"no": 1,
"name": "jack",
"age": 19,
"isAdult": true
};
console.log(stu);
console.log(stu.age);
var stu2 = [{
"no": 1,
"name": "jack",
"age": 19,
"isAdult": true
}, {
"no": 2,
"name": "tom",
"age": 17,
"isAdult": false
}];
console.log(stu2[1].no);
var stus = {
"num": 2,
"msg": [{
"no": 1,
"name": "jack",
"age": 19,
"isAdult": true
}, {
"no": 2,
"name": "tom",
"age": 17,
"isAdult": false
}]
};
console.log(stus.msg[0].isAdult);
stus.msg.forEach(function(data, index, array) {
console.log(data);
console.log(index);
});
var myStr = '[{"no": 1,"name": "jack","age": 19,"isAdult": true}]';
console.log(myStr);
console.log(JSON.parse(myStr));
</script>
</html>