Blogger Information
Blog 28
fans 0
comment 1
visits 13372
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串和数组常用API
centos
Original
375 people have browsed it

字符串和数组常用API

字符串API

  1. <script>
  2. // 字符串
  3. let str = "ABc,123上学去";
  4. // 长度
  5. console.log(str.length);
  6. // 索引
  7. // 索引->元素
  8. console.log(str[8]);
  9. console.log(str.charAt(8));
  10. // 元素->索引
  11. console.log(str.indexOf("上"));
  12. console.log(str.search("去"));
  13. // 拼装
  14. console.log(str.concat("啦啦啦"));
  15. console.log(str + "哈哈哈");
  16. console.log(`${str}嘻嘻嘻`);
  17. // 替换replace
  18. console.log(str.replace("123", "我"));
  19. document.body.append(str.replace("123", "我"));
  20. // 取子串
  21. console.log(str.slice(1, 7));
  22. // 知道取几个
  23. console.log(str.substr(3, 4));
  24. // 从后边取
  25. console.log(str.substr(-4, 4));
  26. // 变大写或者变小写
  27. console.log(str.toLowerCase());
  28. console.log(str.toUpperCase());
  29. // 加a链接
  30. console.log(str.link("https://baidu.com"));
  31. document.body.insertAdjacentHTML(
  32. "afterBegin",
  33. str.link("https://baidu.com")
  34. );
  35. // 加粗
  36. console.log(str.bold());
  37. // document.body.append(str.bold());
  38. document.body.insertAdjacentHTML("afterBegin", str.bold());
  39. </script>

数组API

  1. <script>
  2. // 数组
  3. // 字面量
  4. let arr = [
  5. 1,
  6. 3,
  7. 4,
  8. "我",
  9. function () {
  10. return 1;
  11. },
  12. { x: 0.2, y: 0.5 },
  13. ];
  14. console.log("-----------------------------");
  15. console.log(arr);
  16. console.log(Array.of(...arr));
  17. // 类数组
  18. let jon = {
  19. 0: "x",
  20. 1: "y",
  21. 2: "z",
  22. length: 3,
  23. };
  24. console.log(Array.from(jon));
  25. // 数组两端增删
  26. // push\pop\unshift\shift
  27. const myArray = [1, 5, 78, 34, 67, 99];
  28. //查找
  29. // filter find findIdex;
  30. console.log(
  31. myArray.filter(function (item) {
  32. return item >= 20;
  33. })
  34. );
  35. console.log(myArray.find((item) => item >= 20));
  36. console.log(myArray.findIndex((item) => item >= 20));
  37. // 归并reduce
  38. res = myArray.reduce(function (acc, item, index, myArray) {
  39. return acc + item;
  40. }, 100);
  41. console.log(res);
  42. //遍历
  43. let arr1 = myArray.forEach(
  44. (item, index, arr) => (arr[index] = item * 10)
  45. );
  46. console.log(arr1);
  47. console.log(myArray);
  48. let arr2 = myArray.map((item) => item * 2);
  49. console.log("-------------------");
  50. console.log(arr2);
  51. console.log(myArray);
  52. //正序反序排列sort
  53. console.log(myArray.sort((a, b) => a - b));
  54. console.log(myArray.sort((a, b) => b - a));
  55. // 数组转字符串toString
  56. // 数组的slice
  57. console.log(myArray.slice(1, 4));
  58. // 删除新增替换splice
  59. // 默认删除
  60. console.log(myArray.splice(1, 2));
  61. console.log(myArray);
  62. // 新增
  63. console.log(myArray.splice(1, 0, "张先生"));
  64. console.log(myArray);
  65. // 替换
  66. console.log(myArray.splice(1, 1, "李先生"));
  67. console.log(myArray);
  68. </script>
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