Blogger Information
Blog 19
fans 0
comment 0
visits 9836
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串api和数组api及遍历
newbie
Original
580 people have browsed it

代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>字符串api和数组api实例演示</title>
  8. </head>
  9. <body>
  10. <script>
  11. let xu = "许可可博客";
  12. //获取字符串长度
  13. console.log(xu.length);
  14. //按照索引方式获取字符元素'可'
  15. console.log(xu[2]);
  16. //按照元素方式获取索引值
  17. console.log(xu.indexOf("可"));
  18. //从某个位置开始取几个字符 支持负数 也就是从后往前取
  19. console.log(xu.substr(1, 3)); //从索引[1]开始取3个
  20. // 字符串替换 必须要相邻的字符 前面是被替换的后面是替换后的
  21. console.log(xu.replace("许可", "xukeke"));
  22. //数组
  23. //数组的两种遍历方式
  24. let arr = [1, 2, 3, 4, 5, 6, 7, 8];
  25. console.log(arr.length);
  26. // forEach方法
  27. let res = arr.forEach((itme, index, arr) =>
  28. console.log(itme, index, arr)
  29. );
  30. console.log(res);
  31. // map方法
  32. res2 = arr.map((itme) => itme * 5);
  33. console.log(res2);
  34. //every:全部满足条件就返回ture
  35. console.log(arr.every((itme) => itme >= 0));
  36. console.log(arr.every((itme) => itme >= 2));
  37. // some:只有有一个满足条件就返回ture
  38. console.log(arr.some((itme) => itme >= 7));
  39. console.log(arr.some((itme) => itme >= 2));
  40. //返回符合条件的第一个元素 find
  41. console.log(arr.find((itme) => itme >= 9));
  42. console.log(arr.find((itme) => itme >= 8));
  43. </script>
  44. </body>
  45. </html>

效果图

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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
Author's latest blog post