[Vuejs]-V-for loop in vue.js

1👍

I’d use a computed property that returns an array containing…

  1. Only a / Module1 if it exists
  2. The entire exampleObject / checklist.types otherwise

Then you can just iterate that computed property

export default {
  data: () => ({
    checklist: {
      types: [/* whatever */]
    }
  }),
  computed: {
    modules: ({ checklist: { types } }) => {
      const module1 = types.find(({ Name }) => Name === "Module1")
      return module1 ? [ module1 ] : types
    }
  }
}
<div v-for="module in modules" :key="module.Name">
  <span>{{ module.Name }}</span>
</div>

Leave a comment