js 数据的一些操作
// 数字,字符串类型转字符串
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(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 }
我传入的数组格式是:
[
{
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就可以了