优化精简这段代码 currentStoreDistance() { const { longitude, latitude, big_stores } = this.data const stores = big_stores const origin = `${longitude},${latitude}` const destination = `${stores.longitude},${stores.latitude}` const that=this this.data.myAmapFun.getDrivingRoute({ origin, destination, success(data) { stores.distance = Math.round(data.paths[0].distance / 100) / 10 that.setData({ big_stores:stores }) } }) }
时间: 2024-02-13 10:06:37 浏览: 21
可以优化的地方有:
1. 将 destructuring 和赋值合并成一行;
2. 不需要将 `big_stores` 赋值给 `stores`,直接使用 `big_stores` 即可;
3. 将 `const that = this` 改为箭头函数;
4. 使用对象解构简化 `data.paths[0].distance` 的访问;
5. 使用模板字符串简化字符串拼接。
优化后的代码如下:
```
currentStoreDistance() {
const { longitude, latitude, big_stores } = this.data;
const origin = `${longitude},${latitude}`;
const destination = `${big_stores.longitude},${big_stores.latitude}`;
this.data.myAmapFun.getDrivingRoute({
origin,
destination,
success: (data) => {
const { distance } = data.paths[0];
big_stores.distance = Math.round(distance / 100) / 10;
this.setData({ big_stores });
}
});
}
```
相关问题
优化以下代码 if (ConfigsData.ContainsKey("BUOY_LIST")) { //二级变量,存储BUOYLIST里面的所有数据 JsonData jsondataMessage = ConfigsData["BUOY_LIST"]; for (int i = 0; i < jsondataMessage.Count; i++) { JsonData message = jsondataMessage[i]; BUOY buoy = new BUOY() { BUOY_NAME = message.GetStringValue("BUOY_NAME"), LONGITUDE = message.GetDoubleValue("LONGITUDE"), LATITUDE = message.GetDoubleValue("LATITUDE"), IN_SCREEN = message.GetBoolValue("IN_SCREEN"), CAMERA_POSITION_X = message.GetDoubleValue("CAMERA_POSITION_X"), CAMERA_POSITION_Y = message.GetDoubleValue("CAMERA_POSITION_Y"), CAMERA_POSITION_Z = message.GetDoubleValue("CAMERA_POSITION_Z"), CAMERA_ROTITION_X = message.GetDoubleValue("CAMERA_ROTITION_X"), CAMERA_ROTITION_Y = message.GetDoubleValue("CAMERA_ROTITION_Y"), }; if (jsondataMessage[i].ContainsKey("IS_RED_COLOR")) { buoy.IS_RED_COLOR = message.GetBoolValue("IS_RED_COLOR"); } BUOY_LIST.Add(buoy); } }
以下是对代码的优化建议:
1. 将字符串常量提取为常量或者变量,避免多次使用字符串常量时的性能损失。
2. 使用 TryGetValue 方法来获取字典中的值,避免重复访问字典。
3. 将循环中的 jsondataMessage[i] 提取为变量,避免多次重复访问。
4. 对于包含可选字段的情况,使用 TryGetValue 方法来获取可选字段的值。
优化后的代码如下所示:
```
const string BUOY_LIST_KEY = "BUOY_LIST";
if (ConfigsData.TryGetValue(BUOY_LIST_KEY, out var buoyListData))
{
foreach (var buoyData in buoyListData)
{
var buoy = new BUOY()
{
BUOY_NAME = buoyData.GetStringValue("BUOY_NAME"),
LONGITUDE = buoyData.GetDoubleValue("LONGITUDE"),
LATITUDE = buoyData.GetDoubleValue("LATITUDE"),
IN_SCREEN = buoyData.GetBoolValue("IN_SCREEN"),
CAMERA_POSITION_X = buoyData.GetDoubleValue("CAMERA_POSITION_X"),
CAMERA_POSITION_Y = buoyData.GetDoubleValue("CAMERA_POSITION_Y"),
CAMERA_POSITION_Z = buoyData.GetDoubleValue("CAMERA_POSITION_Z"),
CAMERA_ROTITION_X = buoyData.GetDoubleValue("CAMERA_ROTITION_X"),
CAMERA_ROTITION_Y = buoyData.GetDoubleValue("CAMERA_ROTITION_Y"),
};
buoyData.TryGetValue("IS_RED_COLOR", out buoy.IS_RED_COLOR);
BUOY_LIST.Add(buoy);
}
}
```
优化这个方法 calculateDistance(){ const that=this const mapVar=JSON.parse(JSON.stringify(that.data.map)) mapVar.forEach(item=>{ const origin=that.data.longitude+','+that.data.latitude const destination=item.longitude+','+item.latitude myAmapFun.getDrivingRoute({ origin: origin, destination: destination, success: function(data){ item.distance=Math.round(data.paths[0].distance/100)/10 }, }); }) that.setData({ map:mapVar }) console.log(this.data.map) console.log(Date()) }
这个方法中使用了异步操作,因此我们需要等所有异步操作完成后才能更新数据并显示结果。我们可以使用 Promise.all() 方法来等待所有异步操作完成后再更新数据。
下面是优化后的代码:
```javascript
calculateDistance() {
const that = this;
const mapVar = JSON.parse(JSON.stringify(that.data.map));
const promises = [];
mapVar.forEach(item => {
const origin = that.data.longitude + ',' + that.data.latitude;
const destination = item.longitude + ',' + item.latitude;
const promise = new Promise((resolve, reject) => {
myAmapFun.getDrivingRoute({
origin: origin,
destination: destination,
success: function(data) {
item.distance = Math.round(data.paths[0].distance / 100) / 10;
resolve();
},
fail: function(err) {
reject(err);
}
});
});
promises.push(promise);
});
Promise.all(promises).then(() => {
that.setData({
map: mapVar
});
console.log(that.data.map);
console.log(Date());
}).catch(err => {
console.error(err);
});
}
```
在上面的代码中,我们使用了 Promise.all() 方法来等待所有异步操作完成后再更新数据。具体来说,我们将所有异步操作的 Promise 对象存储在一个数组中,并使用 Promise.all() 方法来等待这些异步操作完成后执行回调函数。
在每个异步操作的回调函数中,我们使用 resolve() 方法来指示该异步操作已完成,并将返回的数据保存到 mapVar 数组中。在所有异步操作完成后,我们使用 setData() 方法来更新数据并显示结果。
另外,我们还使用了 Promise 的错误处理机制来处理异步操作失败的情况,并在 catch() 方法中输出错误信息。
总的来说,通过使用 Promise.all() 方法,我们可以更加优雅地处理异步操作,并使得代码更加简洁易懂。