functionpercentage(source, n =0){var arr =JSON.parse(JSON.stringify(source));var product =[];var total =0;for(var i =0; i < arr.length; i++){
total += arr[i];}for(var i =0; i < arr.length; i++){var val =(arr[i]/ total *100).toFixed(n);
val =parseFloat(val)
product.push(val);}return product;}// 第二个值是保留小数位 返回 => 数组数字类型 单位需要拼接percentage([30,30,40,90],1);// [15.8, 15.8, 21.1, 47.4] => ['15.8%', '15.8%', '21.1%', '47.4']
Math_Integer(base){const q = Math.floor((base /10))*10;const w = base %10;const e = Math.ceil((w /10))*10;const r = q + e;return r <=0?0: r <10?10: r ;}// 第二个值是保留小数位Math_Integer(87);// 90Math_Integer(40);// 40Math_Integer(1);// 10Math_Integer(0);// 0
时间格式化
Date.prototype.Format=function(fmt){var o ={"M+":this.getMonth()+1,//月份 "D+":this.getDate(),//日 "h+":this.getHours(),//小时 "m+":this.getMinutes(),//分 "s+":this.getSeconds(),//秒 "q+": Math.floor((this.getMonth()+3)/3),//季度 "S":this.getMilliseconds()//毫秒 };if(/(y+)/i.test(fmt)){
fmt = fmt.replace(RegExp.$1,reg=>String(this.getFullYear()).substr(4- RegExp.$1.length));}for(var k in o){if(newRegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1,reg=> reg.length ===1? o[k]:(("00"+ o[k]).substr(String(o[k]).length)));}}return fmt;}// 挂载在date原型上的时间格式化newDate().Format("YYYY-MM-DD hh:mm:ss.S")// 2025-12-30 13:44:30.373 精确到毫秒constOS=Object.prototype.toString;functionisDate(obj){returnOS.call(obj)==='[object Date]';}functionisString(obj){returnOS.call(obj)==='[object String]';}functiongetClass(el){return el.getAttribute('class')|| el.getAttribute('className');}// 获取平年闰年 平年为True
Date.isLeap=function(y){return(y %100!==0&& y %4===0)||(y %400===0);};// 判断两者两者时间是否是相同 年月日 返回值booleanDate.prototype.isSame=function(y, m, d){if(isDate(y)){var dt = y;
y = dt.getFullYear();
m = dt.getMonth()+1;
d = dt.getDate();}returnthis.getFullYear()=== y &&this.getMonth()+1=== m &&this.getDate()=== d;}// 给Date添加天Date.prototype.add=function(n){this.setDate(this.getDate()+ n);returnthis;}// 给Date扣除天Date.prototype.minus=function(n){this.setDate(this.getDate()- n);}// 获取年月的天数
Date.getDaysNum=function(y, m){var num =31;switch(m){case2:
num =this.isLeap(y)?29:28;break;case4:case6:case9:case11:
num =30;break;}return num;}
Date.getDaysNum(2025,12)// 31// 三个参数,年月 和需要添加的月的个数
Date.getSiblingsMonth=function(y, m, n){var d =newDate(y, m -1);
d.setMonth(m -1+ n);return{y: d.getFullYear(),m: d.getMonth()+1};// return new Date(d.getFullYear(), d.getMonth() + 1)}
Date.getSiblingsMonth(2025,11,3)// { y: 2026, m: 2 }// 三个参数,年月 和需要添加的月的个数 n 默认为1 可不传
Date.getNextMonth=function(y, m, n){returnthis.getSiblingsMonth(y, m, n ||1);}
console.dir(Date.getNextMonth(2025,5,8));// // { y: 2026, m: 1 }// 三个参数,年月 和需要扣除的月的个数
Date.getPrevMonth=function(y, m, n){returnthis.getSiblingsMonth(y, m,0-(n ||1));}
console.dir(Date.getPrevMonth(2025,5,1));// { y: 2025, m: 4 }
// 冒泡排序functionsort(arr){
arr.forEach((v, i)=>{
arr.forEach((vv, ii)=>{if(arr[i]< arr[ii + i]){let temp = arr[i];
arr[i]= arr[ii + i];
arr[ii + i]= temp;}})})return arr;}functionquicksort(arr){if(arr.length <=1)return arr;var pivot = arr[0];var big =[];var small =[];for(let i =1; i < arr.length; i++){if(arr[i]> pivot){
big.push(arr[i]);}else{
small.push(arr[i]);}}return[...quicksort(small), pivot,...quicksort(big)]}
// 数组扁平化var arr =[[[[1,2,3],4,5],6],7,[[[8]],9,10]];functionflatten(arr){var result =[];functioniterator(arr){for(let i =0; i < arr.length; i++){if(Array.isArray(arr[i])){iterator(arr[i]);}else{
result.push(arr[i]);}}}iterator(arr);return result;}
console.log(flatten(arr));
// 原生遍历// filterArray.prototype.filter=function(fn){var result =[];for(var i =0; i <this.length ; i++){if(fn(this[i])){
result.push(this[i]);}}return result;}// mapArray.prototype.map=function(fn){var result =[];for(var i =0; i <this.length ; i++){
result.push(fn(this[i]));}return result;}var arr =[1,2,3,4,5,6,7];
console.log(arr.filter(item=> item %2==0));var arr =[1,2,3,4,5,6,7];
console.log(arr.map(item=> item *2));
// 查询字符串 and 反查询字符串queryStr(obj){let r =[];for(let i in obj) r.push(obj[i]+'='+ i);return r.join('&');},reQueryStr(str){let r = str.split('&');let o ={};for(let i =0; i < r.length; i++){let key = r[i].split('=')[0];let val = r[i].split('=')[1];
o[key]= val;};return o;}