[{latitude:"36.292042",longitude: "120.315876",id:'333},{latitude:"36.292042",longitude: "120.315876",id:'444},{latitude:"36.292042",longitude: "120.315876",id:'555},]把经纬度取出来形成一个新数组
时间: 2023-07-02 08:06:49 浏览: 44
好的,让我来帮您处理一下这个问题。您可以使用 JavaScript 的 map() 方法和解构赋值来实现取出经纬度并形成新数组的操作,代码如下:
```
const data = [
{ latitude: "36.292042", longitude: "120.315876", id: '333' },
{ latitude: "36.292042", longitude: "120.315876", id: '444' },
{ latitude: "36.292042", longitude: "120.315876", id: '555' },
];
const result = data.map(({ latitude, longitude }) => ({ latitude, longitude }));
console.log(result); // [{ latitude: "36.292042", longitude: "120.315876" }, { latitude: "36.292042", longitude: "120.315876" }, { latitude: "36.292042", longitude: "120.315876" }]
```
经过以上处理,将会得到包含经纬度的新数组。希望我的回答能够帮到您,有什么其他问题欢迎继续提问哦!
相关问题
检查优化此段代码:Page({ /** * 页面的初始数据 */ data: { //经纬度 latitude: 45.745842, longitude: 127.212255, markers: [{ latitude: 45.745842, longitude: 127.212255, width: 35, height: 50 }] }, onLoad: function() { var that = this; wx.getLocation({ type: 'gcj02', success: function(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, }); } }); }, onSearchInputChange: function(event) { var searchValue = event.detail.value; if (searchValue) { wx.getLocation({ type: 'gcj02', success: function(res) { var latitude = res.latitude; var longitude = res.longitude; var marker = { id: 0, latitude: latitude, longitude: longitude, width: 50, height: 50, }; this.setData({ longitude: longitude, latitude: latitude, markers: [marker] }); } }); } } })
可以将代码进行如下优化:
Page({
/**
* 页面的初始数据
*/
data: {
// 经纬度
latitude: 45.745842,
longitude: 127.212255,
markers: [{
latitude: 45.745842,
longitude: 127.212255,
width: 35,
height: 50
}]
},
onLoad() {
wx.getLocation({
type: 'gcj02',
success: res => {
this.setData({
latitude: res.latitude,
longitude: res.longitude,
});
}
});
},
onSearchInputChange(event) {
const searchValue = event.detail.value;
if (!searchValue) {
return;
}
wx.getLocation({
type: 'gcj02',
success: res => {
const latitude = res.latitude;
const longitude = res.longitude;
const marker = {
id: 0,
latitude,
longitude,
width: 50,
height: 50,
};
this.setData({
longitude,
latitude,
markers: [marker]
});
}
});
}
})
主要优化点如下:
1. 使用箭头函数来替代传统的 function,可以避免 this 指向问题。
2. 减少不必要的 if 嵌套,可以使代码看起来更加简洁。
3. 将重复的代码进行封装,提高代码的复用性。
4. 使用 const 或 let 来替代 var,避免变量声明提升问题。
buttonTap: { wx.getLocation({ type:'gcj02', success:function (res) { wx.openLocation({ latitude: res.latitude, longitude: res.longitude, }) } }) } data: { latitude:"45.743291" ,longitude="127.2126" markers:[{ iconPath:'/images/navi.png', id:0, latitude:"45.743291" ,longitude="127.2126", width:50, height:50 }] } markertap: function() { wx.openLocation({ latitude: this.data.latitude, longitude: this.data.longitude, name:'哈尔滨信息工程学院', address:'' }) } function()报错
这段代码中存在一些语法错误和逻辑错误,可能会导致程序运行出错:
1. 在data对象中,longitude的赋值符号应该是冒号而不是等号,应该改为:
```
data: {
latitude: "45.743291",
longitude: "127.2126",
markers: [{
iconPath: '/images/navi.png',
id: 0,
latitude: "45.743291",
longitude: "127.2126",
width: 50,
height: 50
}]
}
```
2. buttonTap事件中调用了wx.getLocation方法,但是该方法是异步的,因此需要使用回调函数来获取位置信息。可以将wx.getLocation的调用放到一个单独的函数中,并在成功获取位置信息后调用wx.openLocation方法。改进后的代码如下:
```
buttonTap: function() {
var that = this;
wx.getLocation({
type: 'gcj02',
success: function(res) {
that.openLocation(res.latitude, res.longitude);
}
})
},
openLocation: function(latitude, longitude) {
wx.openLocation({
latitude: latitude,
longitude: longitude,
name: '哈尔滨信息工程学院',
address: ''
})
},
```
3. markertap事件中调用了wx.openLocation方法,但是传入的参数不正确,应该是this.data.markers[0].latitude和this.data.markers[0].longitude,而不是this.data.latitude和this.data.longitude。改进后的代码如下:
```
markertap: function() {
wx.openLocation({
latitude: this.data.markers[0].latitude,
longitude: this.data.markers[0].longitude,
name: '哈尔滨信息工程学院',
address: ''
})
},
```
希望这些改动能够解决你的问题。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)