JavaScript复习之内置对象,JSON

88 阅读2分钟

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>
		/* 
			String对象:
				length:长度
				indexOf():指定字符在字符串中第一次出现的位置
				lastIndexOf():指定字符在字符串中最后一次出现的位置
				substring():截取字符串
					一个参数:截取到末尾
					两个参数:含头不含尾
				substr(m,n):截取字符串
					两个参数:从索引为m开始,截取n个
				split():拆分字符串
						
		 */
		var content = 'We are family are we!!!';
		console.log(content.length); //23
		console.log(content.indexOf('a')); //3
		console.log(content.lastIndexOf('a')); //14
		console.log(content.substring(3)); //are family are we!!!
		console.log(content.substring(4, 8)); //re f
		console.log(content.substr(4, 8)); // re famil
		console.log(content.toUpperCase()); // 全大写
		console.log(content.toLowerCase()); // 全小写
		var str = 'id:1,name:tom,age:18';
		// 获取tom
		console.log(str.split(',')[1].split(':')[1]); //tom
	</script>
</html>

Math对象

基础介绍

ceil():向上取整
floor():向下取整
abs():绝对值
round():四舍五入
random():随机数  [0,1)  ***    扩大倍数、移动区间

案例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
	<script>
		/* 
			Math对象:
				ceil():向上取整
				floor():向下取整
				abs():绝对值
				round():四舍五入
				random():随机数  [0,1)  ***    扩大倍数、移动区间
				
		 */
		var myNum=13.14;
		console.log(Math.ceil(myNum));//14
		console.log(Math.floor(myNum));//13
		console.log(Math.abs(myNum));//13.14
		console.log(Math.round(myNum));//13
		console.log(Math.random());// [0,1)
		
		// 需求:[1,4]     随机整数 1,2,3,4    Math.random()*4+1
		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>
		/* 
			Array对象:
				push():末尾追加
				pop():末尾删除
				unshift:头部添加
				shift():头部删除
				splice():删除 添加
					splice(m,n,p):从索引为m位置,删除n个内容,添加内容p
					splice(m,n):从索引为m位置,删除n个内容
				join():
					拼接成字符串
					没有参数:默认使用逗号拼接
					传入参数:使用指定内容拼接
		 */
		var myArr = [10, 2, 30, 4, 15];
		// myArr.push(1);
		// myArr.pop();
		// myArr.unshift(30);
		// myArr.shift();

		// splice(m,n,p):删除 添加,从索引为m位置,删除n个内容,添加内容p
		// myArr.splice(1,3,2);
		// splice(m,n):从索引为m位置,删除n个内容
		// myArr.splice(1,3);
		console.log(myArr.join());//10,2,30,4,15
		console.log(myArr.join('-'));//10-2-30-4-15
		
	</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>
		/* 
			Date对象:
				getFullYear():年份值 4位整数
				getMonth():月份 0~11
				getDate():日 1~31
				getHours():小时 0~59
				getMinutes():分钟 0~59
				getSeconds():秒 0~59
				getDay():星期 0~6
		 
		 */
		var nowDate = new Date();
		console.log(nowDate); //Fri Aug 12 2022 16:52:27 GMT+0800 (中国标准时间)
		/* nowDate = new Date('2023-9-12 16:59:34');
		console.log(nowDate); //Tue Sep 12 2023 16:59:34 GMT+0800 (中国标准时间)
		nowDate = new Date('2023/9/12 16:59:34');
		console.log(nowDate); //Tue Sep 12 2023 16:59:34 GMT+0800 (中国标准时间) */
		
		// 年份
		console.log(nowDate.getFullYear());//2022
		// 月份 0~11
		console.log(nowDate.getMonth());//7
		// 日 1~31  一月中的某一天
		console.log(nowDate.getDate());//12
		// 小时 0~23
		console.log(nowDate.getHours());//16
		//分钟 0~59
		console.log(nowDate.getMinutes());//57
		// 秒数 0~59
		console.log(nowDate.getSeconds());
		// 星期 0~6 一周中的某一天
		console.log(nowDate.getDay());//5
		
	</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>
		/* 
			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对象		
		 */
		var stu = {
			"no": 1,
			"name": "jack",
			"age": 19,
			"isAdult": true
		};
		console.log(stu);
		// 19
		console.log(stu.age);
		// 数组嵌套JSON
		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
			}]
		};
		// 获取msg
		console.log(stus.msg[0].isAdult); //true
		// 遍历数据
		/**
		 * @param {Object} data 遍历到的每一项数据  ***
		 * @param {Object} index 遍历到的每一项数据对应的索引值  idx ***
		 * @param {Object} array 数组对象
		 */
		stus.msg.forEach(function(data, index, array) {
			console.log(data);
			console.log(index);
			// console.log(array);
		});
		// 假设->服务端传回来的字符串值->转换为JSON对象
		var myStr = '[{"no": 1,"name": "jack","age": 19,"isAdult": true}]';
		console.log(myStr);
		// parse():将字符串格式的值转换为JSON对象
		console.log(JSON.parse(myStr));

		// 简化写法
		/* var stu2 = {
			no: 1,
			name: "jack",
			age: 19,
			isAdult: true
		};
		console.log(stu2); */
	</script>
</html>