VueJs如何在API中从变量中分离字符串



我是VueJs新手。我想用逗号(,)分割字符串,并将其保存到另一个变量。这个字符串来自API。这是我的API,我使用json server

"featured-property": [
{
"id": "1",
"name": "Property 1",
"address": "Address Property 1",
"geo": "0.5307596, 101.4461512"
},
{
"id": "2",
"name": "Property 2",
"address": "Address Property 2",
"geo": "0.5055971577036701, 101.452919"
}

我想分割"geo"然后保存到变量"lat"还有"长",这样我就可以用它来浏览"谷歌地图"了。我使用axios来获取API。

export default{
name: 'PropertyDetail',
data(){
return{
property: [],
}
},
methods: {
setProperty(data){
this.property = data
},
},
mounted() {
axios
.get("http://localhost:3000/featured-property")
.then((response) => this.setProperty(response.data))
.catch((error) => console.log(error))
}
}

如何用不同的方式去做?谢谢. .

如果数据从JSON中为您提供了对象数组,那么当您循环遍历数组时,您可以使用这样的函数;

saveObject: function(object){
var latLngArray = object.geo.split(', ');
object["lat"] = latLngArray[0];
object["lng"] = latLngArray[1];
}

在setProperty函数中,你可以这样调用

setProperty(data){
var arrayProperty = [];
var self = this;
data.forEach(function(item){
arrayProperty.push(self.saveObject(item));
})
this.property = arrayProperty;
}

saveObject函数将处理您传入的数据。这不是理想的方式。最好创建一个与传入对象相同的变量并返回该变量

最新更新