小左闲谈

JS获取数组的最大值和最小值

当前位置:首页 » javascript
In 笔记  @2017-03-30

项目中遇到的需求:
高德轨迹返回的字符串为一连串的坐标点及海拔等数据,现在需要计算所有坐标点海拔最大落差;
也就是海拔的最大值和最小值的差值。
接口返回的json数据result是这样的:

{
    "id":0,
    "msg":"获取数据",
    "data":{
        "id":"2246",
        "record_id":"1",
        "startpoint":"29.012560221354168,120.47038384331597,gps,1490840902590,0.0,306.11",
        "endpoint":"29.011363,120.471702,gps,1490841850974,0.0,404.71",                    
        "pathline":"29.012560221354168,120.47038384331597,gps,1490840902606,0.0,306.11;29.01255425347222,120.47040256076389,gps,1490840905591,0.0,309.87;29.012554796006945,120.47040635850695,gps,1490840908583,0.0,312.38;29.01253824869792,120.47041314019097,gps,1490840911591,0.0,314.79;29.01252007378472,120.47042507595486,gps,1490840914608,0.0,314.65;29.01252007378472,120.47042507595486,gps,1490840917643,0.0,312.49;29.01251681857639,120.47042507595486,gps,1490840920586,0.39,311.22;29.012509223090277,120.47042236328124,gps,1490840923603,0.26,311.05;29.012479654947917,120.4704248046875,gps,1490840926583,1.05,311.11;29.01244384765625,120.47043891059027,gps,1490840929603,1.17,311.09;29.012423502604168,120.47045003255208,gps,1490840932589,1.06,311.03;29.01241021050347,120.4704652235243,gps,1490840935589,1.0,311.17;29.012403428819443,120.47046983506945,gps,1490840938608,0.92,311.55;29.012401801215276,120.47048963758681,gps,1490840941609,0.92,311.8;29.012410481770832,120.47051595052083,gps,1490840944610,0.9,311.75;29.01242513020833,120.47055284288194,gps,1490840947602,0.93,311.54;29.012442762586804,120.47056830512153,gps,1490840950592,0.87,311.39;29.012456868489583,120.47059326171875,gps,1490840953581,0.86,311.18;29.012467719184027,120.47059868706597,gps,1490840956594,0.62,311.0;29.012483452690972,120.4706165907118,gps,1490840959599,0.57,310.9;29.0125146484375,120.47065565321181,gps,1490840962600,1.01,310.85;29.01253200954861,120.4706787109375,gps,1490840965590,1.01,310.82;29.012550184461805,120.4707066514757,gps,1490840968591,0.94,310.82;29.01257378472222,120.47071831597222,gps,1490840971590,0.9,310.91;29.012590874565973,120.47073432074653,gps,1490840974587,0.88,310.97;29.01260064019097,120.47074245876736,gps,1490840977610,0.86,310.98;29.012616916232638,120.47076904296875,gps,1490840981592,0.63,310.95;29.01262234157986,120.47078342013889,gps,1490840984601,0.55,311.05;29.012636990017363,120.47081081814235,gps,1490840987587,0.79,311.31;29.012650282118056,120.4708251953125,gps,1490840990590,0.63,311.81;29.012664116753474,120.47084065755209,gps,1490840993587,0.67,312.35;29.01266899956597,120.47085693359375,gps,1490840996607,0.36,312.81;29.012671712239584,120.47087673611111,gps,1490840999585,0.32,313.29;29.01267822265625,120.47088785807291,gps,1490841002585,0.33,313.99;29.012677680121527,120.47089545355902,gps,1490841005585,0.14,314.49;29.012679036458334,120.47088758680556,gps,1490841008594,0.49,314.83;29.012674153645833,120.47088568793403,gps,1490841011591,0.06,315.02;",
        "travel_notes_title":"# 步行 # ",
        "travel_notes_img_count":"4",
        "distance":"1.913",
        "averagespeed":"7.26",
        "duration":"948",
        "audio_count":"0",
        "video_count":"0",
        "kacl":"66"
    }
}

result数据中的pathline轨迹坐标点数据只截取了一部分,js数据处理代码如下:

  1.     var pathline = result.data.pathline;
  2.     var info_arr = pathline.split(';');
  3.     var altitude = [];// 海拔数据集合
  4.     var point_arr = [];// 坐标点集合
  5.     for(i=0;i<info_arr.length-1;i++){
  6.         var arr = info_arr[i].split(',');
  7.         point_arr[i] = [arr[1],arr[0]];
  8.         altitude[i] = arr[5];
  9.     }
  10.     console.log(altitude);
  11.     console.log(Math.max.apply(null, altitude));
  12.     console.log(Math.min.apply(null, altitude));
  13.     //计算海拔最大落差
  14.     var high = Math.max.apply(null, altitude) - Math.min.apply(null, altitude);
  15.     high = high.toFixed(2);

altitude为包括全部坐标点的数据集合;
获取最大值Math.max.apply(null, altitude),最小值使用Math.max.apply(null, altitude)。
输出的结果是:

["306.11", "309.87", "312.38", "314.79", "314.65", "312.49", "311.22", "311.05", "311.11", "311.09", "311.03", "311.17", "311.55", "311.8", "311.75", "311.54", "311.39", "311.18", "311.0", "310.9", "310.85", "310.82", "310.82", "310.91", "310.97", "310.98", "310.95", "311.05", "311.31", "311.81", "312.35", "312.81", "313.29", "313.99", "314.49", "314.83", "315.02"]
315.02
306.11
标签: , ,   评论: 抢沙发  被围观 233 次

js将时间秒转换成天,小时,分钟,秒的字符串

当前位置:首页 » javascript
In 笔记  @2017-03-28

项目中需求是这样,接口返回的数据中时间单位为秒,但前端显示的时候需要更人性化的带有单位(天,小时,分钟,秒)的字符串;
转换函数如下:

  1. /**
  2.  * 格式化秒
  3.  * @param   int     value   总秒数
  4.  * @return  string  result  格式化后的字符串
  5.  */
  6. function formatSeconds(value) { 
  7.     var theTime = parseInt(value);// 需要转换的时间秒 
  8.     var theTime1 = 0;// 分 
  9.     var theTime2 = 0;// 小时 
  10.     var theTime3 = 0;// 天
  11.     if(theTime > 60) { 
  12.         theTime1 = parseInt(theTime/60); 
  13.         theTime = parseInt(theTime%60); 
  14.         if(theTime1 > 60) { 
  15.             theTime2 = parseInt(theTime1/60); 
  16.             theTime1 = parseInt(theTime1%60); 
  17.             if(theTime2 > 24){
  18.                 //大于24小时
  19.                 theTime3 = parseInt(theTime2/24);
  20.                 theTime2 = parseInt(theTime2%24);
  21.             }
  22.         } 
  23.     } 
  24.     var result = '';
  25.     if(theTime > 0){
  26.         result = ""+parseInt(theTime)+"秒";
  27.     }
  28.     if(theTime1 > 0) { 
  29.         result = ""+parseInt(theTime1)+"分"+result; 
  30.     } 
  31.     if(theTime2 > 0) { 
  32.         result = ""+parseInt(theTime2)+"小时"+result; 
  33.     } 
  34.     if(theTime3 > 0) { 
  35.         result = ""+parseInt(theTime3)+"天"+result; 
  36.     }
  37.     return result; 
  38. }
标签: ,   评论: 1枚  被围观 523 次

JS根据Unix时间戳显示发布时间是多久前

当前位置:首页 » javascript
In 笔记  @2016-10-27

后台接口给的时间数据为Unix时间戳,我们的需求是显示类似微信朋友圈显示发布时间为距离当前时间多久之前,“xx分钟之前”,“xx小时之前”,“xx个月之前”。
类似这样的时间显示效果:
时间转换
转换函数:

  1. /**
  2.  * Unix时间戳转换为当前时间多久之前
  3.  * @param timespan 	int 		Unix时间戳
  4.  * @return timeSpanStr 	string 		转换之后的前台需要的字符串
  5.  */
  6. function Ftime (timespan) {
  7.     var dateTime = new Date(timespan * 1000);
  8.     var year = dateTime.getFullYear();
  9.     var month = dateTime.getMonth() + 1;
  10.     var day = dateTime.getDate();
  11.     var hour = dateTime.getHours();
  12.     var minute = dateTime.getMinutes();
  13.     //当前时间
  14.     var now = Date.parse(new Date());  //typescript转换写法
  15.     var milliseconds = 0;
  16.     var timeSpanStr;
  17.     //计算时间差
  18.     milliseconds = (now / 1000) - timespan;
  19.  
  20.     //一分钟以内
  21.     if (milliseconds <= 60) {
  22.         timeSpanStr = '刚刚';
  23.     }
  24.     //大于一分钟小于一小时
  25.     else if (60 < milliseconds && milliseconds <= 60 * 60) {
  26.         timeSpanStr = Math.ceil((milliseconds / (60))) + '分钟前';
  27.     }
  28.     //大于一小时小于等于一天
  29.     else if (60 * 60 < milliseconds && milliseconds <= 60 * 60 * 24) {
  30.         timeSpanStr = Math.ceil(milliseconds / (60 * 60)) + '小时前';
  31.     }
  32.     //大于一天小于等于15天
  33.     else if (60 * 60 * 24 < milliseconds && milliseconds <= 60 * 60 * 24 * 30) {
  34.         timeSpanStr = Math.ceil(milliseconds / (60 * 60 * 24)) + '天前';
  35.     }
  36.     //大于一个月小于一年
  37.     else if (60 * 60 * 24 * 30 < milliseconds && milliseconds <= 60 * 60 * 24 * 30 * 12){
  38.         timeSpanStr = Math.ceil(milliseconds / (60 * 60 * 24 * 30)) + '个月前';
  39.     }
  40.     //超过一年显示
  41.     else {
  42.         timeSpanStr = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute;   
  43.     }
  44.     return timeSpanStr;
  45. }

项目实测。

标签: , ,   评论: 抢沙发  被围观 1,193 次