arrays - 使用 v-for 在数组中显示数组的正确方法
问题描述
我正在尝试显示文本数组/对象的值,但我得到一个输出,试图向我显示数组/对象中每个名称的段落。
链接到当前结果:当前结果
我对 vue.js 还很陌生,所以欢迎任何提示!
<template>
<div class="education center">
<div v-if="object.timelines != null">
<template v-for="(time,index) in object.timelines">
<p :key="index">{{ time.schoolyear }}</p>
<div :key="index" v-bind="time" v-for="(text,index) in time.text">
<p :key="text">Degree: {{ text.degree }}</p>
<p>Institution: {{ text.institution }}</p>
<p>Where: {{text.where}}</p>
</div>
</template>
</div>
</div>
</template>
<script>
export default {
el: ".education",
data: function() {
return {
object: {
timelines: [
{
schoolyear: "2016 - 2017",
text: [
{ degree: "Applied Computer Science" },
{ institution: "Thomas More University of Applied Science" },
{ where: "Belgium, Geel" }
]
},
{
schoolyear: "2018 - 2019",
text: [
{ degree: "Business IT" },
{ institution: "HAMK University of Applied Science" },
{ where: "Finland, Hämeenlinna" }
]
}
]
}
};
}
};
</script>
我只想为 schoolyear="2016 - 2017" 显示一次 text.degree
解决方案
我并不完全清楚你想要什么,但也许是你想要遍历.text
数组,并且对于数组中的每个条目,显示它的键和它的内容。如果是这样,您可以尝试以下方法:
<p v-for="(entry, index) in time.text" :key="index">
{{Object.keys(entry)[0]}}: {{Object.values(entry)[0]}}
</p>
如果您需要担心键的标题大小写,您可以使用计算属性来转换数组,或者定义一个方法来转换每个字符串。
推荐阅读
- .net - EF Core/Npgsql 参数化整个 where 子句
- flutter - Flutter Assets Audio Player 播放下一首歌曲
- flutter - Flutter Dropdown:“对象?”类型的值 不能分配给“字符串”类型的变量。- “对象”来自“飞镖:核心”
- python - 如何识别tensorflow网络运行缓慢和内存不足的原因
- apache-spark - Pyspark GCP UnsupportedOperationException:org.apache.parquet.column.values.dictionary.PlainValuesDictionary$PlainDoubleDictionary
- python - 从 3D 数组中查找每个 2D 数组中最小值的索引
- sql - 使用 SQL 开发人员插入时缺少逗号错误
- sharepoint-online - SharePoint GetItems 在 4kb 处被截断?
- java - Java中split函数中大写S的计数机制
- amazon-web-services - Cloudfront 是否禁用/启用有效地“使”所有文件“无效”?