我们可以使用for循环或者forEach()方法来遍历一个对象数组,然后利用字符串模板(Template literals)来格式化字符串。
代码示例:
const students = [{name: '小明', age: 18}, {name: '小红', age: 20}, {name: '小刚', age: 22}];
// 使用 for 循环遍历
for(let i=0; i
// 使用 forEach() 方法遍历
students.forEach((student) => {
console.log(姓名:${student.name}, 年龄:${student.age}
);
});
输出结果: 姓名:小明, 年龄:18 姓名:小红, 年龄:20 姓名:小刚, 年龄:22
上一篇:遍历对象数组并根据属性进行筛选
下一篇:遍历对象数组并获取新的对象数组。