// 1.创建数组对象 var arr01 = new Array(); // 2.压入数据 arr01.push("apple"); arr01.push("orange"); arr01.push("banana"); arr01.push("grape"); // 3.遍历数组 for (var i = 0; i < arr01.length; i++) { console.log(arr01[i]); } // 4.数组元素反序 arr01.reverse(); for (var i = 0; i < arr01.length; i++) { console.log(arr01[i]); } // 5.数组元素拼接成字符串 var arrStr = arr01.join(","); console.log(arrStr); // 6.字符串拆分成数组 var arr02 = arrStr.split(","); for (var i = 0; i < arr02.length; i++) { console.log(arr02[i]); } // 7.弹出数组中最后一个元素 var ele = arr01.pop(); console.log(ele);
// 8.使用[]创建数组 var arr03 = ["cat","dog","tiger"]; console.log(arr03);
在开发中凡是涉及到『跨平台数据传输』,JSON格式一定是首选。
{key:value,key:value,...,key:value}1
[value,value,...,value]1
正因为JSON格式中value部分还可以继续使用JSON对象或JSON数组,所以JSON格式是可以『多层嵌套』的,所以JSON格式不论多么复杂的数据类型都可以表达。
{ "stuId":556, "stuName":"carl", "school":{ "schoolId":339, "schoolName":"atguigu" }, "subjectList":[ { "subjectName":"java", "subjectScore":50 }, { "subjectName":"PHP", "subjectScore":35 }, { "subjectName":"python", "subjectScore":24 } ], "teacherMap":{ "aaa":{ "teacherName":"zhangsan", "teacherAge":20 }, "bbb":{ "teacherName":"zhangsanfeng", "teacherAge":108 }, "ccc":{ "teacherName":"zhangwuji", "teacherAge":25 } } }
var jsonObj = {"stuName":"tom","stuAge":20}; var jsonStr = JSON.stringify(jsonObj); console.log(typeof jsonObj); // object console.log(typeof jsonStr); // string
jsonObj = JSON.parse(jsonStr); console.log(jsonObj); // {stuName: "tom", stuAge: 20}