登录  /  注册
博主信息
博文 38
粉丝 0
评论 0
访问量 24573
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
字符串和数组的api演示
Blackeye
原创
474人浏览过

hw

  1. <!DOCTYPE html>
  2. <html lang="en">
  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</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 自选不少于5个字符串和数组的api实例演示,特别是数组遍历方法
  12. // 字符串
  13. let str = "php中文网";
  14. // 1.字符串长度
  15. out(str.length);
  16. // 2.索引获取元素
  17. out(str.charAt(0));
  18. // 3.元素获取索引
  19. out(str.indexOf("文"));
  20. out(str.search("文"));
  21. // 4.字符串拼装
  22. out(str.concat(' php.cn'));
  23. // 5.字符串替换
  24. out(str.replace("中文网",".cn"));
  25. // 6.字符串查询
  26. out(str.slice(0,3));
  27. // 7.substr
  28. out(str.substr(-3,3));
  29. // 8.split分割字符串为数组
  30. out(str.replace("中文网",".cn").split('.'));
  31. ///////////////////////////////////////////////////////////////////////
  32. // 数组
  33. let arr = [1,2,3,4,5,6,7,8,9];
  34. // 1.foreach,Map
  35. arr.forEach((item,index)=>{out(`${index}:${item}`)});
  36. out(arr.map((item,index)=>`index:${index},item:${item}`));
  37. // 2.every,some
  38. out(arr.every(item=>item>=3));
  39. out(arr.every(item=>item>=1));
  40. out(arr.some(item=>item>=3));
  41. out(arr.some(item=>item>=1));
  42. // 3.filter,find,findIndex
  43. out(arr.filter(item=>item>=3));
  44. out(arr.find(item=>item>=3));
  45. out(arr.findIndex(value=>value>=8));
  46. out(arr.findIndex(value=>value>=10));
  47. // 4.reduce
  48. out(arr.reduce((res,index)=>res+index,0));
  49. out(arr.reduce((res,index)=>res+index,100));
  50. // ConsoleOutHelper
  51. function out(outprint){
  52. return console.log(outprint);
  53. }
  54. </script>
  55. </body>
  56. </html>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学