Blogger Information
Blog 24
fans 0
comment 0
visits 18490
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js数据类型数组、对象以及json类型的常用操作
昔年
Original
710 people have browsed it

1.数组

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var course = ['php', 'js', 'css', 'java','c++'];
  11. console.log(course.length);
  12. console.log(course[2]);
  13. console.log(Array.isArray(course));
  14. //遍历一个数组
  15. for(var i = 0; i <course.length; i++){
  16. console.log(course[i]);
  17. }
  18. course.forEach(function(item, index, array){
  19. document.body.innerHTML += "<li>" + index + item + "</li>";
  20. })
  21. document.body.innerHTML += "<hr>";
  22. //使用slice获取数组元素
  23. console.log(course.slice(0,2));
  24. console.log(course.slice(0));
  25. //使用splice实现数组元素的删除,插入,替换
  26. course.splice(2, 0,'html');
  27. console.log(course);
  28. course.splice(2, 1,'go');
  29. console.log(course);
  30. course.splice(2,1);
  31. console.log(course);
  32. </script>
  33. </body>
  34. </html>

2.对象

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var staff = {
  11. message:{
  12. id: 108,
  13. name:'peter',
  14. },
  15. department:'office',
  16. grade:'primary',
  17. postion:'php开发工程师',
  18. salary:8000,
  19. }
  20. console.table(staff);
  21. console.log(staff['message'].name);
  22. for (key in staff){
  23. console.log(staff[key]);
  24. }
  25. </script>
  26. </body>
  27. </html>

3.将js的对象转换为json格式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var staff = {
  11. message:{
  12. id: 108,
  13. name:'peter',
  14. },
  15. department:'office',
  16. grade:'primary',
  17. postion:'php开发工程师',
  18. salary:8000,
  19. sex:undefined,
  20. }
  21. var jsonStr = JSON.stringify(staff);
  22. console.log(jsonStr);
  23. var jsonStr = JSON.stringify(staff,['department','postion']);
  24. console.log(jsonStr);
  25. jsonStr = JSON.stringify(staff,function(key, value){
  26. switch (key) {
  27. case 'message':
  28. return '这是我的秘密';
  29. break;
  30. case 'salary':
  31. return '10000';
  32. default:
  33. return value;
  34. }
  35. });
  36. console.log(jsonStr);
  37. </script>
  38. </body>
  39. </html>

总结:js中的数组和PHP中的索引数组类似,js中的对象和PHP中的关联数组类似。json格式在前后端交互时用的非常多。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:js中要记忆的东西比php多太多, 一定要多写多记,没有捷径
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!