gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
vue3 取对象列表中的一个字段转成字符串
- User
vue3 取对象列表中的一个字段转成字符串
- 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 }}输出计算属性的结果。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.