gpt4 book ai didi

javascript - 将对象添加到新字符串时获取 "undefined"

转载 作者:行者123 更新时间:2023-11-28 14:35:16 26 4
gpt4 key购买 nike

我有一个对象数组(学生姓名、成就等),我想在我创建的新空数组中添加任何名为“Jody”(例如)的对象。但是,当我尝试仅打印新对象数组的名称时,我得到了未定义。注意:我不能使用 let、const 等。

我在这里缺少什么想法吗?

var student;
var newStudents = [];
var students = [{
name: 'Odelia',
track: 'Accounting',
achievements: '26',
points: '2260'
}, {
name: 'Jody',
track: 'Web Design',
achievements: '12',
points: '890'
}, {
name: 'Yann',
track: 'Javascript',
achievements: '10',
points: '2266'
}, {
name: 'Max',
track: 'Marketing',
achievements: '13',
points: '1010'
}, {
name: 'Jody',
track: 'iOS',
achievements: '9',
points: '1002'
}, ]

for (var i = 0; i < students.length; i++) {
student = students[i];
if (student.name === 'Jody') {
newStudents.push(student);
}
}

console.log(newStudent.name);

输出:

    0: 
achievements: "12"
name: "Jody"
points: "890"
track: "Web Design"
__proto__: Object
1:
achievements: "9"
name: "Jody"
points: "1002"
track: "iOS"
__proto__: Object
length: 2
__proto__: Array(0)

undefined

最佳答案

应该是newStudents而不是newStudent

    console.log(newStudents[0].name);

此外,newStudents 是数组。

关于javascript - 将对象添加到新字符串时获取 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49997876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com