我有一個物件,其中包含我想根據類別顯示的資料對。我相信這與嵌套的 v-for 回圈有關,但我無法找出解決此問題的正確方法。
categoryArray = {stage 1, red}, {stage 1, blue}, {stage 2, orange}, {stage 3, brown}, {stage 2, green}
所需顯示: 第 1 階段:紅色、藍色 第 2 階段:橙色 第 3 階段:棕色
我還沒有可用的代碼。我的策略是創建一系列獨特的階段,用它來顯示階段,但我不知道如何迭代階段內的專案。
獲取階段:
let stagesArray = [];
categoryArray.value.forEach((entry, index) => {
stagesArray.push(entry.stageNumber);
});
//creating array of unique stages
uniqueRooms.value = [...new Set(stagesArray)];
})
上面的作業是為了獲得唯一的房間陣列。
<template>
<div v-for="(stage, index) in uniqueRooms" :key="index">
{{ room }}
<div v-for="(color, index) in filterStages" :key="index">
{{ color }}
</div>
</div>
</template>
<script>
//this is a mess and I don't know where to go.
const filterStages = computed(function (stage) {
return stagesUnique.value.forEach((stageNumber) => {
return categoriesArray.value.filter((color) => color.stage
=== stageNumber);
});
</script>
我出去滑雪了。我只需要一些關于如何使用唯一值回圈遍歷主類別(階段)的線索,然后顯示該類別中的所有匹配顏色。
這似乎非常接近,但我無法弄清楚從中獲得獨特階段的方法。Vue 中的嵌套回圈
uj5u.com熱心網友回復:
這是你要找的嗎?
const { createApp, computed } = Vue;
createApp({
setup() {
const data = [
{ stage: 1, color: 'red' },
{ stage: 1, color: 'blue' },
{ stage: 2, color: 'orange' },
{ stage: 3, color: 'brown' },
{ stage: 2, color: 'green' }
];
const stages = computed(
() => [...new Set(data.map(({ stage }) => stage))]
);
const stageColors = computed(
() => s => data.filter(({ stage }) => stage === s)
.map(({ color }) => color)
);
return {
stages,
stageColors
}
}
}).mount('#app');
h4 { margin-bottom: 0;}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
<div v-for="stage in stages" :key="stage">
<h4>Stage #{{ stage }}</h4>
<ul>
<li v-for="color in stageColors(stage)" :key="color" v-text="color" />
</ul>
<hr>
</div>
</div>
uj5u.com熱心網友回復:
你有兩個回圈的相同索引,所以這是錯誤的
<div v-for="(stage, index) in uniqueRooms" :key="index">
----
{{ room }}
<div v-for="(color, index) in filterStages" :key="index">
-----
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/364161.html
標籤:javascript Vue.js 类星体
