Blogger Information
Blog 33
fans 0
comment 0
visits 17122
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串与数组的 api实例演示
lucaslwk
Original
336 people have browsed it

字符串与数组的 api 实例演示

一.字符串 api

  1. let str = "苟利国家生死以,岂因祸福避趋之";
  2. //长度
  3. console.log(str.length);
  4. //索引->元素
  5. console.log(str[2]);
  6. //元素->索引
  7. console.log(str.indexOf("国"));
  8. //indexOf性能更高,search支持正则表达式
  9. console.log(str.search("[国生]"));
  10. //字符串拼装
  11. console.log(str.concat("--林则徐"));
  12. console.log(`${str}--林则徐`);
  13. //替换
  14. console.log(str.replace(",", "_"));
  15. //取子串
  16. //开始位置,结束位置,忽略结束索引对应的值
  17. console.log(str.slice(0, 7));
  18. //开始位置,子串长度,支持负数
  19. console.log(str.substr(0, 7));
  20. console.log(str.substr(8, 7));
  21. console.log(str.substr(-7, 7));
  22. let s = "abc-EFG-12345";
  23. //使用指定分隔符分割成字符串数组
  24. console.log(s.split("-"));
  25. console.log(s.split("", 7));
  26. //大小写
  27. console.log(s.toUpperCase());
  28. console.log(s.toLowerCase());
  29. //转换为超链接
  30. document.body.insertAdjacentHTML(
  31. "afterbegin",
  32. s.link("http://www.hao123.com/")
  33. );

二.数组 api

  1. //返回数组
  2. let arr = Array.of(1, 2, 3, 4, 5);
  3. console.log(arr);
  4. //类数组转换
  5. let li = document.querySelector("ul").children;
  6. console.log(li);
  7. console.log(Array.from(li));
  8. //前后端的增删
  9. //后端添加
  10. arr.push(6, 7);
  11. // 前端添加
  12. arr.unshift(-1, 0);
  13. console.log(arr);
  14. // 后端删除
  15. arr.pop();
  16. // 前端删除
  17. arr.shift();
  18. console.log(arr);
  19. //数组的遍历
  20. //arr.forEach(function(元素的值,值对应的索引,数组本身) {对数组中的每个元素逐个调用回调方式进行处理})
  21. // 三个参数,只有第一个必选的,其它可不写,返回undefined
  22. let res = arr.forEach((item) => (item <= 3 ? console.log(item) : false));
  23. console.log(res);
  24. //需要返回值时使用map
  25. res = arr.forEach((item) => item * 2);
  26. console.log(res);
  27. res = arr.map((item) => item * 2);
  28. console.log(res);
  29. //every,数组全部满足条件才会返回true
  30. console.log(arr.every((item) => item >= 3));
  31. //some,数组部分满足条件就可以返回true
  32. console.log(arr.some((item) => item >= 3));
  33. //filter,只返回为真元素组成的数组
  34. console.log(arr.filter((item) => item <= 3));
  35. //find,返回满足条件的第一个
  36. console.log(arr.find((item) => item <= 3));
  37. //归并
  38. // arr.reduce(function(累加器,元素的值,值对应的索引,数组本身) {},初始值)
  39. res = arr.reduce((sum, item) => {
  40. console.log(`sum:${sum},item:${item}`);
  41. return sum + item;
  42. }, 1000);
  43. console.log(res);
  44. //累加器
  45. res = arr.reduce((sum, item) => sum + item);
  46. console.log(res);
  47. //排序sort
  48. let pai = [3, 14, 15, 92, 65];
  49. //依照字符串顺序排序
  50. console.log(pai.sort());
  51. //asc,小到大
  52. console.log(pai.sort((a, b) => a - b));
  53. //dsc,大到小
  54. console.log(pai.sort((a, b) => b - a));
  55. //拼接join
  56. pai = [3, 14, 15, 92, 65];
  57. console.log(pai.join());
  58. console.log(pai.join(","));
  59. console.log(pai.join("_"));
  60. //取子元素slice
  61. console.log(pai.slice(2, 4));
  62. console.log(pai.slice(-3, -1));
  63. //splice默认删除,也可以替换,新增
  64. //splice(从索引a的地方开始,删除b个元素,插入c,d,e,f...)
  65. //删除,删除元素的索引,返回被删除的元素
  66. console.log(pai.splice(3, 2));
  67. console.log(pai);
  68. //替换,替换元素的索引,更新值
  69. console.log(pai.splice(1, 2, 92, 65));
  70. console.log(pai);
  71. //新增,添加的位置,新增值
  72. console.log(pai.splice(1, 0, 14, 15));
  73. console.log(pai);
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