Blogger Information
Blog 119
fans 3
comment 1
visits 94292
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS 对象模拟数组
赵大叔
Original
688 people have browsed it

使用js对象模拟数组代码

  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>对象模拟数组</title>
  8. </head>
  9. <body>
  10. </body>
  11. </html>
  12. <script>
  13. function MyArr() {
  14. // arguments压缩可变数量参数为一个类数组
  15. this.length = arguments.length;
  16. for(var i = 0; i < arguments.length; i++) {
  17. this[i] = arguments[i];
  18. }
  19. // 1、新元素将添加在数组的末尾,并返回新的长度
  20. this.push = function (item){
  21. this[this.length] = item;
  22. this.length++;
  23. return this.length;
  24. }
  25. // 2、删除数组的最后一个元素并返回删除的元素
  26. this.pop = function (){
  27. var popArr = this[this.length - 1];
  28. delete this[this.length - 1];
  29. this.length--;
  30. return popArr;
  31. }
  32. // 3、返回表示Integer值的String对象
  33. this.toString = function (){
  34. var result = "";
  35. var j = ',';
  36. for (var i = 0; i < this.length - 1; i++){
  37. result += this[i];
  38. result += j;
  39. }
  40. result += this[i];
  41. return result;
  42. }
  43. // 4、对数组的元素进行排序:正序和倒序
  44. this.sort = function sort(arr, flag = true) {
  45. for(var i = 0; i < arr.length - 1; i++) {
  46. for (var j = 0; j < arr.length - i - 1; j++) {
  47. if(flag) {
  48. if (arr[j] > arr[j + 1]) {
  49. var swap = arr[j];
  50. arr[j] = arr[j + 1];
  51. arr[j + 1] = swap;
  52. }
  53. }else{
  54. if (arr[j] < arr[j + 1]) {
  55. var swap = arr[j];
  56. arr[j] = arr[j + 1];
  57. arr[j + 1] = swap;
  58. }
  59. }
  60. }
  61. }
  62. return arr;
  63. }
  64. // 5、返回数组参数中的最大值
  65. this.max = function arrmax(arr) {
  66. var max = arr[0];
  67. for(var i = 0; i < arr.length; i++) {
  68. if(arr[i] > max)
  69. max = arr[i];
  70. }
  71. return max;
  72. }
  73. // 6、返回数组参数中的最小值
  74. this.min = function arrmin(arr) {
  75. var min = arr[0];
  76. for(var i = 0; i < arr.length; i++) {
  77. if(arr[i]< min)
  78. min = arr[i];
  79. }
  80. return min;
  81. }
  82. // 7、反转数组中元素的顺序
  83. this.reverse = function() {
  84. var result = [];
  85. for(var i = 0; i < this.length; i++) {
  86. result[result.length] = this[this.length - i - 1];
  87. }
  88. for(var i = 0; i < result.length; i++) {
  89. this[i] = result[i];
  90. }
  91. return this;
  92. }
  93. }
  94. var arr = new MyArr(11,3,55,88,99,"aaa");
  95. // 添加在数组的末尾
  96. console.log(arr.push("help"));
  97. // 删除数组的最后一个元素
  98. console.log(arr.pop());
  99. // Integer值的String对象
  100. console.log(arr.toString());
  101. // 输出结果是开头第一个的元素
  102. console.log(arr[0]);
  103. // 排序:true时为正序,false时为倒序
  104. console.log(arr.sort(arr, false));
  105. // 返回数组参数中的最大值
  106. console.log(arr.max(arr));
  107. // 返回数组参数中的最小值
  108. console.log(arr.min(arr));
  109. // 反转数组中元素的顺序
  110. console.log(arr.reverse());
  111. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!