Blogger Information
Blog 13
fans 0
comment 0
visits 6605
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串和数组api操作
shooter
Original
548 people have browsed it

字符串API

效果图

代码

  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. let str = 'php中文网';
  12. //长度
  13. console.log(str.length);
  14. //索引->元素
  15. console.log(str[3]);
  16. //元素->索引
  17. console.log(str.indexOf('网'));
  18. console.log(str.search('网'));
  19. //字符串拼装
  20. console.log(str.concat('<a>', 'php.cn', '<a>'));
  21. console.log(str + 'php.cn');
  22. console.log(`${str}php.cn`);
  23. //php中文网 -> php.cn
  24. console.log(str.replace('中文网', '.cn'));
  25. //slice 取子串,忽略结束索引对应的值
  26. console.log(str.slice(0, 3));
  27. //substr: 不知到哪结束,但是知道取多少
  28. console.log(str.substr(0, 3));
  29. console.log(str.substr(3, 3));
  30. console.log(str.substr(0, 3) + str.substr(3, 3));
  31. //从后往前取,取出‘中文网’
  32. console.log(str.substr(-3, 3));
  33. //split : string -> array
  34. let s = '1-2-3-4';
  35. console.log(s.split('-'));
  36. console.log(str.split('', 6));
  37. //toLowerCase , toUpperCase 大小写转换
  38. console.log(str.toLocaleLowerCase());
  39. console.log(str.toLocaleUpperCase());
  40. // 字符串link方式,字符串的内容会成为a标签的文本值
  41. console.log(str.link('https://php.cn'));
  42. document.body.insertAdjacentHTML(
  43. 'afterbegin',
  44. str.link('https://php.cn')
  45. );
  46. console.log(str.bold());
  47. document.body.insertAdjacentHTML('afterbegin', str.bold());
  48. </script>
  49. </body>
  50. </html>

数组api

效果图

代码

  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. //字面量
  12. let arr = [
  13. 1,
  14. 2,
  15. 'a',
  16. 'b',
  17. true,
  18. [1, 2, 3],
  19. {
  20. x: 1,
  21. y: 2,
  22. },
  23. function () {},
  24. ];
  25. console.log(arr);
  26. //array.of
  27. //服务器返回数组
  28. let a = [1, 2, 3, 4, 5];
  29. console.log(Array.of(1, 2, 3, 4, 5));
  30. console.log(Array.of(...a));
  31. //类数组
  32. const arr1 = {
  33. 0: 'red',
  34. 1: 'blue',
  35. 2: 'green',
  36. length: 3,
  37. };
  38. //数组本质上是一个对象
  39. console.log(arr1);
  40. //转为真正的数组
  41. console.log(Array.from(arr1));
  42. //增删仅限于数组二端的操作
  43. //push,pop,unshift,shift
  44. let arr2 = [];
  45. console.log(arr2);
  46. //push 尾部追加
  47. //pop 尾部删除
  48. console.log(arr2.push(10));
  49. console.log(arr2.push(20, 30));
  50. console.log(arr2);
  51. console.log(arr2.pop());
  52. console.log(arr2.pop());
  53. console.log(arr2.pop());
  54. console.log(arr2.pop());
  55. // unsift :头部追加
  56. //shift:头部删除
  57. console.log(arr2);
  58. console.log(arr2.unshift(10));
  59. console.log(arr2.unshift(30, 20));
  60. console.log(arr2);
  61. //push + shift,就像是排队,尾部进入,头部删除
  62. //遍历数组
  63. //1.forEach,map
  64. //2.every,some
  65. //3.filter,find,findIndex
  66. //4.reduce
  67. let arr3 = [1, 2, 3, 4, 5];
  68. // arr.forEach(function(元素的值,值对应的索引,数组本身) {
  69. // 对数组中的每个元素逐个调用回调方式进行处理
  70. // })
  71. // 三个参数,只有第一个必选的,其它可不写
  72. let res = arr3.forEach((item, index, arr3) =>
  73. console.log(item, index, arr3)
  74. );
  75. console.log(res);
  76. //需要返回值,就用map
  77. res = arr3.map((item, index, arr3) => [item, index, arr3]);
  78. //将每个元素乘二返回一个新数组,[1,2,3]=>[2,4,6]
  79. res = arr3.map((item) => item * 2);
  80. console.log(res);
  81. //every,some
  82. //every:将数组成员全部满足条件,则返回 true,否则 false
  83. console.log(arr3.every((item) => item >= 0));
  84. console.log(arr3.every((item) => item >= 3));
  85. console.log(arr3.every((item) => item >= 6));
  86. console.log(arr3.every((item) => item >= 3));
  87. //filter,只返回为真的元素组成员的数组
  88. console.log(arr3.filter((item) => item >= 3));
  89. //find,返回满足条件的第一个
  90. console.log(arr3.find((item) => item >= 3));
  91. //归并 reduce
  92. let acc = 0;
  93. for (let i = 0; i < arr3.length; i++) {
  94. acc += arr3[i];
  95. }
  96. console.log(acc);
  97. // arr.reduce(function(累加器,元素的值,值对应的索引,数组本身) {})
  98. res = arr3.reduce(function (acc, item, index, arr3) {
  99. console.log(`acc=${acc}, item=${item}`);
  100. return acc + item;
  101. }, 1000);
  102. //acc:结果累加器
  103. res = arr3.reduce((acc, item) => acc + item);
  104. console.log(res);
  105. //sort
  106. let arr4 = [1, 10, 20, 6];
  107. //sort,默认按字符串来排
  108. console.log(arr4.sort());
  109. //升序
  110. console.log(arr4.sort((a, b) => a - b));
  111. //降序
  112. console.log(arr4.sort((a, b) => b - a));
  113. // join: array -> string
  114. // string: split
  115. arr5 = ['red', 'green', 'blue'];
  116. console.log(arr5.join());
  117. console.log(arr5.join('--'));
  118. //slice:取子元素,字符串也有一个slice
  119. arr6 = [1, 2, 3, 4, 5, 6, 7, 8];
  120. console.log(arr6);
  121. console.log(arr6.slice(2, 5));
  122. console.log(arr6.slice(2));
  123. //-6,-3 ==>2,5
  124. console.log(arr6.slice(-6, -3));
  125. //splce:删除,新增,替换
  126. //默认删除,返回被删除的的元素
  127. //2,3
  128. console.log(arr6.splice(1, 2));
  129. console.log(arr6);
  130. //更新
  131. //4,5被 a,b替换,被更新了
  132. console.log(arr6.splice(1, 2, 'a', 'b'));
  133. console.log(arr6);
  134. //新增,不删除
  135. console.log(arr6.splice(1, 0, 'red', 'green'));
  136. console.log(arr6);
  137. //反转
  138. //reverse() 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。
  139. let arr7 = [1, 2, 3];
  140. console.log(arr7);
  141. console.log(arr7.reverse(arr7));
  142. </script>
  143. </body>
  144. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!