The problem of item value in vue v-for Loop

problem description:
I want to reuse a written public component

<template>
    <ul>        
      <li v-for="(item, index) in items" :key=index>
        <div>
          <h2>{{item.name}}</h2>
        </div>
      </li>
    </ul>
</template>

the data structures of two items are as follows:

items1: [
  { name: "a" },
  { name: "b" },
  { name: "c" }
]

items2: [
  { data: { name: "a" } },
  { data: { name: "b" } },
  { data: { name: "c" } }
]

when you upload items1, you get name: item.name,
when you send items2, you get name: item.data.name
except that the data structure is slightly different, other contents are the same, so you want to reuse this component

.

my idea:
write two ul in this component, and then use a logo to control whether the display is item.name or item.data.name, but I think this method is too stupid, it is no different from writing a new component.

I would like to ask: is there anyone who has encountered this kind of situation, and how do we solve it (not to mention discussing with the background to change the data structure to be consistent? )

Apr.28,2021

I wonder if this is possible

<template>
    <ul>        
      <li v-for="(item, index) in items" :key=index>
        <div>
          <h2>{{item.data?item.data.name:item.name}}</h2>
        </div>
      </li>
    </ul>
</template>

write a computed as compatibility layer

computed: {
    fItem () {
        if (this.item.length == 0)
            return [];
            
        if (this.item[0].data) {
            return this.item.map((v) => v.data);
        } else {
            return this.item;
        }
    }
}

then loop: fItem is fine


use computed to construct a fixed format array according to the format of items, and then use this array of computed to loop out components to


change one of the arrays so that the format of the two arrays is the same?


you don't need three yuan. Look at this

{{ item.data || item.data.name }}

@ the map of the original ceremony _ Error302 is more reliable
I feel that you should manually make the data format compatible with the component, not let the component be compatible with the data format
because the data format is always changing, and the display effect of the component to the data is the same:

    <vfortemp :data='items1'></vfortemp>
    <vfortemp :data='items2.map(i => i.data)'></vfortemp>
Menu