Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

vue3 取对象列表中的一个字段转成字符串

  1. User

    vue3 取对象列表中的一个字段转成字符串

  2. Assistant

    可以使用 Vue3 的 computed 计算属性和 map 方法来取对象列表中的一个字段转成字符串。

    <template>
      <div>
        <p>{{ str }}</p>
      </div>
    </template>
    
    <script>
    import { computed } from 'vue';
    
    export default {
      data() {
        return {
          list: [
            { id: 1, name: 'Alice' },
            { id: 2, name: 'Bob' },
            { id: 3, name: 'Carol' },
          ],
          field: 'name',
        };
      },
      computed: {
        str() {
          return this.list.map((item) => item[this.field]).join(', ');
        },
      },
    };
    </script>
    

    在上面的例子中,list 是对象列表,field 是要取的字段名,使用 computed 创建一个名为 str 的计算属性,在计算属性中使用 map 方法遍历 list 中每个对象,取出对象的 field 字段值,最后使用 join 方法将所有的字段值拼接成一个字符串。在模板中可以直接使用 {{ str }} 输出计算属性的结果。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.