common.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. Array.prototype.indexOf = function (val, property) {
  2. for (var i = 0; i < this.length; i++) {
  3. if (!property && this[i] == val) {
  4. return i;
  5. } else if (property && this[i][property] == val) {
  6. return i;
  7. }
  8. }
  9. return -1;
  10. };
  11. Array.prototype.remove = function (val, property) {
  12. if(!(this instanceof Array)){
  13. return;
  14. }
  15. var index = this.indexOf(val, property);
  16. if (index > -1) {
  17. this.splice(index, 1);
  18. }
  19. };
  20. Array.prototype.unique = function (arr2) {
  21. if(!(this instanceof Array)){
  22. return;
  23. }
  24. for (var i = 0; i < this.length; i++) {
  25. for (var j = 0; j < arr2.length; j++) {
  26. if (this[i] === arr2[j]) {
  27. this.splice(i, 1); //利用splice函数删除元素,从第i个位置,截取长度为1的元素
  28. }
  29. }
  30. }
  31. //alert(arr1.length)
  32. for (var i = 0; i < arr2.length; i++) {
  33. this.push(arr2[i]);
  34. }
  35. return arr1;
  36. };
  37. Array.prototype.array2Obj = function(key, value){
  38. var obj = {};
  39. if(this instanceof Array){
  40. for(var i = 0; i < this.length; i++){
  41. obj[this[i][key]] = value ? this[i][value] : this[i];
  42. }
  43. }
  44. return obj;
  45. }
  46. String.prototype.trim = function() {
  47. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  48. }
  49. if (typeof String.prototype.startsWith != 'function') {
  50. String.prototype.startsWith = function (prefix){
  51. return this.slice(0, prefix.length) === prefix;
  52. };
  53. }
  54. String.prototype.endWith = function(str){
  55. if(str == null || str == "" || this.length == 0 || str.length > this.length){
  56. return false;
  57. }
  58. if(this.substring(this.length - str.length) == str){
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. return true;
  64. }
  65. String.prototype.replaceAll = function(s1, s2){
  66. return this.replace(new RegExp(s1, "gm"), s2);
  67. }
  68. loading = function(type) {
  69. }
  70. closeLoading = function() {
  71. }
  72. function shuffle(arr) {
  73. var length = arr.length, randomIndex, temp;
  74. while (length) {
  75. randomIndex = Math.floor(Math.random() * (length--));
  76. temp = arr[randomIndex];
  77. arr[randomIndex] = arr[length];
  78. arr[length] = temp
  79. }
  80. return arr;
  81. }
  82. function addNum(number1, number2) {
  83. var n1, n2, m;
  84. try {
  85. n1 = number1.toString().split(".")[1].length;
  86. } catch(e) {
  87. n1 = 0;
  88. }
  89. try {
  90. n2 = number2.toString().split(".")[1].length;
  91. } catch(e) {
  92. n2 = 0;
  93. }
  94. m = Math.pow(100, Math.max(n1, n2));
  95. return (number1 * m + number2 * m) / m;
  96. }
  97. function randomNumBoth(min, max){
  98. var range = max - min;
  99. var rand = Math.random();
  100. var num = min + Math.round(rand * range); //四舍五入
  101. return num;
  102. }
  103. const _w = () => {
  104. return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
  105. }
  106. const _h = () => {
  107. return (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
  108. }
  109. const dictsConcatAfterOrder = (arr1 = [], arr2 = [], orderProperty = 'order') => {
  110. let arr = [].concat(arr2);
  111. arr1.forEach(item => {
  112. let index = arr.indexOf(item.value, 'value');
  113. if (index != -1) {
  114. arr.splice(index, 1);
  115. }
  116. });
  117. arr = arr1.concat(arr);
  118. if (orderProperty) {
  119. arr.sort((a, b) => {
  120. return a[orderProperty] - b[orderProperty];
  121. })
  122. } else {
  123. arr.sort((a, b) => {
  124. return a - b;
  125. })
  126. }
  127. return arr;
  128. }
  129. const getQuery = name => {
  130. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  131. var r = window.location.search.substr(1).match(reg);
  132. if (r != null) {
  133. return unescape(r[2]);
  134. }
  135. return null;
  136. }