vue 数组中获取单项字段
Vue.js 中,数组中的每一项都是一个对象。因此,要获取数组中单项的某个字段,可以像访问对象属性一样访问它。
步骤:
- 确定数组索引:首先,确定要获取字段的数组项的索引。
- 使用方括号语法:使用方括号语法访问数组项,就像这样:array[index]。
- 点语法:然后,使用点语法访问字段,就像这样:array[index].field。
示例:
const array = [ { id: 1, name: "John" }, { id: 2, name: "Mary" }, ]; // 获取第一项的 name 字段 const firstName = array[0].name; // "John"