如何打印这个对象数组:
vStu1 = {id:"12345", name:"Dave Smith", testScore:10};
vStu2 = {id:"43531", name:"Emma Black", testScore:17};
vStu3 = {id:"97894", name:"Sue Davies", testScore:14};
arrStudents = [vStu1, vStu2, vStu3];
采用以下格式:
ID:99999姓名:XXXXXXXXXX TestScore:99
ID:99999姓名:XXXXXXXXXX TestScore:99
ID:99999姓名:XXXXXXXXXX TestScore:99
答案 0 :(得分:0)
您可以使用foreach迭代数组并使用格式化字符串输出:
arrStudents.forEach(s => console.log(`ID: ${s.id} Name: ...`));
答案 1 :(得分:0)
您需要此功能:
vStu1 = {id:"12345", name:"Dave Smith", testScore:10};
vStu2 = {id:"43531", name:"Emma Black", testScore:17};
vStu3 = {id:"97894", name:"Sue Davies", testScore:14};
arrStudents = [vStu1, vStu2, vStu3];
/**
* result variable is this string:
* "ID: 12345 Name: Dave Smith TestScore: 10
* ID: 43531 Name: Emma Black TestScore: 17
* ID: 97894 Name: Sue Davies TestScore: 14"
*/
const result = arrStudents
.map(student => `ID: ${student.id} Name: ${student.name} TestScore: ${student.testScore}`)
.join('\n');