JScript Array对象的几个原型方法

js 代码
  1. Array.prototype.inArray = function (value) {    
  2. for (var i = 0; i < this.length; i++) {    
  3. if (this[i] === value) {    
  4. return true;    
  5. }    
  6. }    
  7. return false;    
  8. };    
  9.   
  10. Array.prototype.max = function(){    
  11. for (var i = 1, max = this[0]; i < this.length; i++){    
  12. if (max < this[i]) {    
  13. max = this[i];    
  14. }    
  15. return max;    
  16. };    
  17.   
  18. Array.prototype.min = function(){    
  19. for (var i = 1, min = this[0]; i < this.length; i++){    
  20. if (min > this[i]) {    
  21. min = this[i];    
  22. }    
  23. return min;    
  24. };    
  25.   
  26. Array.prototype.indexOf = function(p_var)    
  27. {    
  28. for (var i=0; i<this.length; i++)    
  29. {    
  30. if (this[i] == p_var)    
  31. {    
  32. return(i);    
  33. }    
  34. }    
  35. return(-1);    
  36. }    
  37.   
  38. Array.prototype.exists = function(p_var) {return(this.indexOf(p_var) != -1);}    
  39.   
  40. Array.prototype.queue = function(p_var) {this.push(p_var)}    
  41.   
  42. Array.prototype.dequeue = function() {return(this.shift());}    
  43.   
  44. Array.prototype.removeAt = function(p_iIndex) {return this.splice(p_iIndex, 1);}    
  45.   
  46. Array.prototype.remove = function(o)    
  47. {    
  48. var i = this.indexOf(o);    
  49. if (i>-1)    
  50. {    
  51. this.splice(i,1);    
  52. }    
  53. return (i>-1);    
  54. }    
  55.   
  56. Array.prototype.clear = function()    
  57. {    
  58. var iLength = this.length;    
  59. for (var i=0; i < iLength; i++)    
  60. {    
  61. this.shift();    
  62. }    
  63. }    
  64.   
  65. Array.prototype.addArray = function(p_a)    
  66. {    
  67. if (p_a)    
  68. {    
  69. for (var i=0; i < p_a.length; i++)    
  70. {    
  71. this.push(p_a[i]);    
  72. }    
  73. }    
  74. }    
  75.   
  76. Array.prototype.Unique = function()    
  77. {    
  78. var a = {}; for(var i=0; i<this.length; i++)    
  79. {    
  80. if(typeof a[this[i]] == "undefined")    
  81. a[this[i]] = 1;    
  82. }    
  83. this.length = 0;    
  84. for(var i in a)    
  85. this[this.length] = i;    
  86. return this;    
  87. };    
  88.   
  89. Array.prototype.indexOf = function(obj, fromIndex)    
  90. {    
  91. if (fromIndex == null)    
  92. {    
  93. fromIndex = 0;    
  94. }    
  95. else if (fromIndex < 0)    
  96. {    
  97. fromIndex = Math.max(0, this.length + fromIndex);    
  98. }    
  99.   
  100. for (var i = fromIndex; i < this.length; i++)    
  101. {    
  102. if (this[i] === obj)    
  103. {    
  104. return i;    
  105. }    
  106. }    
  107.   
  108. return-1;    
  109. };    
  110.   
  111. Array.prototype.lastIndexOf = function(obj, fromIndex)    
  112. {    
  113. if (fromIndex == null)    
  114. {    
  115. fromIndex = this.length - 1;    
  116. }    
  117. else if (fromIndex < 0)    
  118. {    
  119. fromIndex=Math.max(0, this.length+fromIndex);    
  120. }    
  121.   
  122. for (var i = fromIndex; i >= 0; i--)    
  123. {    
  124. if (this[i] === obj)    
  125. {    
  126. return i;    
  127. }    
  128. }    
  129.   
  130. return -1;    
  131. };    
  132.   
  133. Array.prototype.insertAt = function(o, i)    
  134. {    
  135. this.splice(i, 0, o);    
  136. };    
  137.   
  138. Array.prototype.insertBefore = function(o, o2)    
  139. {    
  140. var i = this.indexOf(o2);    
  141.   
  142. if (i == -1)    
  143. {    
  144. this.push(o);    
  145. }    
  146. else    
  147. {    
  148. this.splice(i, 0, o);    
  149. }    
  150. };    
  151.   
  152. Array.prototype.remove = function(o)    
  153. {    
  154. var i = this.indexOf(o);    
  155.   
  156. if (i != -1)    
  157. {    
  158. this.splice(i, 1);    
  159. }    
  160. };    
  161.   
  162. Array.prototype.mm=function()    
  163. {    
  164. var a={}, m=0, n="";    
  165. for(var i=0; i<this.length; i++)    
  166. a[this[i]]?++a[this[i]]:a[this[i]]=1;    
  167. for(i in a){m=Math.max(m, a[i]); if(m==a[i]) n=i;}    
  168. return {"variable": n, "times": m};    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值