js 資料的一些操作
阿新 • • 發佈:2021-07-14
// 數字,字串型別轉字串
String()// 字串擷取前幾位字元
需要擷取的字串.substring(0, 幾)// 小數點後保留兩位小數(四捨五入)
var data = 3.1415926535 data.toFixed(2) // 3.14 var data1 = 3.1455926535 data.toFixed(2) // 3.15//陣列排序——從小到大
export const sortArray = (arr) => { var interval = parseInt(arr.length / 2) // 分組間隔設定 while(interval > 0){ for我傳入的陣列格式是: [ { address:"鎮江市新區大港興港西路2號", area:"京口區", city:"鎮江市", distance:"213.13" }, { address:"鎮江市新區大港興港西路2號", area:"京口區", city:"鎮江市", distance:"213.31" }, { address:"鎮江市新區大港興港西路2號", area:"京口區", city:"鎮江市", distance:"216.83" }, { address:"鎮江市新區大港興港西路2號", area:"京口區", city:"鎮江市", distance:"218.02" }, { address:"鎮江市新區大港興港西路2號", area:"京口區", city:"鎮江市", distance:"218.06" } ] 如果是這樣格式: [1, 34, 45, 4, 9, 7, 2] 去掉.distance就可以了(var i = 0; i < arr.length; i ++){ var n = i while(Number(arr[n].distance) < Number(arr[n - interval] ? arr[n - interval].distance : '') && n > 0){ var temp = arr[n] arr[n] = arr[n - interval] arr[n - interval] = temp n = n - interval } } interval= parseInt(interval / 2) } return arr }