一、
- 构建一个数组【“00:00”,"00:05"..."23:55"】的数组
function buildAxis(){
var ary=[];
ary.push("00:00");
var start = Date.UTC(2017,1,1,-8,0,0);
for(var i=0;i<288;i++){
start += 1000*60*5;//(5分钟的毫秒数)
var _date=new Date(start);
var Hours=cal(_date.getHours());
var Minutes=cal(_date.getMinutes());
ary.push(Hours+":"+Minutes);
}
ary.pop();
return ary;
}
function cal(o){
if(o<10&&String(o).length==1){
return "0"+o;
}else{
return o;
}
} - 根据输入的年月份(2017-09),返回月份每一天的数组 【“2017-09-01*******2017-09-30”】注意天数
/**
* @desc 根据月份得出当前日月年天数的数组【2018-06-01,2018-06-02...2018-06-30】。
* @param str
* @return {Array} arr
*/
function getCountDays(str) {
var year=str.substr(0,4);
var month=str.substr(-2);
var curDate = new Date(year,month,0);//如果是0返回最后一天 /* 返回当月的天数 */
var day= curDate.getDate();
var dayArr=[];
for (var k = 1; k <=day; k++){
dayArr.push(str.substr(0,4)+"-"+str.substr(-2)+"-"+add0(k)); }
return dayArr;
}
function add0(o){
if(o<10&&String(o).length==1){
return "0"+o;
}else{
return o;
}
}
var str="2017-06";
var arr=getCountDays(str);
console.log(arr);//["2017-06-01","2017-06-02","2017-06-03"..."2017-06-30"]
附加实例,$.grep筛选
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=SLrOIdVUUI5fbvsZTMqTFshsG993NBAG"></script>
<title>地图展示</title>
<style type="text/css">
img { width: 150px !important;}
</style>
</head>
<body>
</body>
</html>
<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
<script type="text/javascript">
//原数组
var rowData=[
{date:'2017-09-18',fzlMax:0.18,fzlMin:0.12},
{date:'2017-09-20',fzlMax:0.25,fzlMin:0.2},
{date:'2017-09-19',fzlMax:0.22,fzlMin:0.12}
]
//需要的数组格式
var series=[
{
name:'最大负载',
type:"line",
data:[]
},{
name:'最小负载',
type:"line",
data:[]
}
]
//第一步:组成每一天的大数组 dateArr ["2017-09-01"****"2017-09-31"]
window.onload=function(){
var str="2017-09";
var dateArr=getCountDays(str);
//第二部:循环数组
for(var j=0;j<dateArr.length;j++){
var ary=$.grep(rowData,function(n,dIndex){
return (n.date==dateArr[j]);
})
if(ary.length>0){
series[0].data.push(ary[0].fzlMax);
series[1].data.push(ary[0].fzlMin);
}else{
series[0].data.push("-");
series[1].data.push("-");
}
}
console.log(series);
} function add0(o){
if(o<10&&String(o).length==1){
return "0"+o;
}else{
return o;
}
}
function getCountDays(str) {
var year=str.substr(0,4);
var month=str.substr(-2);
var curDate = new Date(year,month,0);//如果是0返回最后一天
/* 返回当月的天数 */
var day= curDate.getDate();
var dayArr=[];
for (var k = 1; k <=day; k++) {
dayArr.push(str.substr(0,4)+"-"+str.substr(-2)+"-"+add0(k));
}
return dayArr;
} </script>
- 两个数组相除
Number.prototype.toMyFixed=function(){
try{
if(Math.abs(this)>1000){
return parseFloat(this.toFixed(0),10);
}else if(Math.abs(this)<1){
return parseFloat(this.toFixed(4),10);
}else{
return parseFloat(this.toFixed(2),10);
}
}catch(e){
return this;
}
}
var arr1=[2,4,6];
var arr2=[1,2,3];
alert(calc(arr1,arr2));
//[2,2,2];两个数组相除
function calc(arr1,arr2){
var arr=[];
for(var i=0;i<arr1.length;i++){
var val=devided(arr1[i],arr2[i]);
arr.push(val);
}
return arr;
}
function devided(topNum,botNum){
if(isNaN(topNum)||isNaN(botNum)||parseFloat(botNum)==0){
return "-";
}else{
return Number(topNum/botNum).toMyFixed();
}
}
3.数组求和
/**
* @desc 对一个数组进行求和。
* @param 数组
* @return {string} sum
*/
function arrSum(arr){
if(!arr||arr.length===0){
return 0;
}
var sum=arr.reduce(function(x,y){
x=isNaN(x)?0:x;
y=isNaN(y)?0:y;
return Number(parseFloat(x)+parseFloat(y)).toFixed(2);
},0)
return sum;
}
4.将样式字符串改成对象 2019/06/12 组态中按钮样式合并用到
var style="width:100px;height:200px;border:1px solid red;border-radius:8px 10px 2px 5px";
console.log(styleToObject(style));//{width:100px,height:200px,border:1px solid red,border-radius:8px 10px 2px 5px}
/**把样式字符串样式转化为{}
*@param style:样式字符串
**/
function styleToObject(style) {
let styleObject = {} if (style && style != '' && style !== 'null') {
const len = style.length
styleObject = style.charAt(len - 1) === ';' ? '{' + style + '}' : '{' + style + ';}' // 统一样式后面以";"结束 // 将字符串转化为json标准字符串
styleObject = styleObject.replace(/{/g, '{\"')
styleObject = styleObject.replace(/:/g, '\":\"')
styleObject = styleObject.replace(/;/g, '\",\"')
styleObject = styleObject.replace(/,"}/g, '}') // 将json标准字符串转化为{}对象
styleObject = JSON.parse(styleObject)
}
return styleObject
}