小左闲谈

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

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
标签: , ,   评论: 抢沙发 

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

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. }
标签: ,   评论: 2枚 

PHP对二维数组按照某个字段的值进行排序的两种方法

In 笔记  @2017-01-11

用PHP对二维数组进行排序:
第一种方法:遍历生成新数组用array_multisort()方法排序;

  1. /**
  2.  * 二维数组的排序
  3.  * @param  array  $arr       需要排序的二维数组
  4.  * @param  string $field     以这个数组的值来排序
  5.  * @param  string $direction 排序规则  SORT_ASC 升序  SORT_DESC降序
  6.  * @return array            排序后的数组
  7.  * @author http://blog.m1910.com/
  8.  */
  9. function  sort_array($arr, $field, $direction = 'SORT_DESC'){ 
  10.     $arrSort = array();  
  11.     foreach($arr AS $uniqid => $row){  
  12.         foreach($row AS $key=>$value){  
  13.             $arrSort[$key][$uniqid] = $value;
  14.         }
  15.     }
  16.     array_multisort($arrSort[$field], constant($direction), $arr);
  17.     return $arr;
  18. }

第二种方法:利用usort()自定义排序进行排序

/**
 * 二维数组的排序
 * @param  array  $arr       需要排序的二维数组
 * @param  string $field     以这个数组的值来排序
 * @return array            排序后的数组
 * @author http://blog.m1910.com/
 * @备注:默认升序,需要降序把$a和$b调换位置就好了
 */
function  sort_array($arr, $field){ 
    usort( $arr, function( $a, $b ) {
        return ($a[$field] - $b[$field]);
	});
    return $arr;
}
标签: , , ,   评论: 8枚