【发布时间】:2018-12-24 13:45:55
【问题描述】:
我是编程新手,我将成为前端开发人员。作为练习的一部分,我正在尝试做一个简单的网络应用程序。
但切中要害。
我在尝试过滤/切片 JSON 对象时卡住了——我从控制台得到的只是:“切片/过滤器不是函数”。我知道这些函数适用于数组,但我不知道如何将 JSON 对象转换为 JSON 数组。我在网上看了很多工作人员,尝试实现但没有成功...
有代码:
<template>
<div class="class">
<section v-if="errored">
<p>We're sorry, we're not able to retrieve this information at the moment, please try back later</p>
</section>
<section v-else>
<div v-if="loading">Loading...</div>
<div class="row" v-else>
<div>
<div v-for="vehicle in filteredVehicles">
<img v-for="car in vehicle.cars" v-bind:src="car.url" v-bind:alt="car.name">
</div>
</div>
</div>
</section>
</div>
<script>
import axios from 'axios';
export default {
data() {
return {
vehicles: [],
loading: true,
errored: false
}
},
mounted() {
axios
.get('https://urlurlurl/vehicles.json')
.then(response => (this.vehicles= response.data))
.catch(error => {
console.log(error)
this.errored = true
})
.finally(() => this.loading = false)
},
computed: {
filteredVehicles: function () {
return this.vehicles.filter(function (v) {
return v.name === ('aaa' || 'bbb')
})
}
}
}
</script>
数据结构示例:
{
"1": {
"firstname": "",
"lastname": "",
"age": {
"1": {
"name": "",
"url": ""
},
"3": {
"name": "",
"url": ""
}
}
},
"2": {
"firstname": "",
"lastname": "",
"age": {
"6": {
"name": "",
"url": ""
},
"7": {
"name": "",
"url": ""
}
}
}
// etc.
}
提前感谢您的帮助。
【问题讨论】:
-
我们需要查看 JSON 的结构才能知道如何转换它。
标签: javascript json vue.js vuejs2 axios