在传递索引作为参数时无法通过使用method()获得Array项 它显示为未定义
export class DataService {
public list = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
]
getList() {
return this.list;
}
update(num, updated) {
let list = this.getList()
console.log(typeof (num))
console.log(this.list[num])
}
答案 0 :(得分:1)
您的数组包含对象,但是您想通过id
进行搜索,这是这些对象的属性。您必须使用filter
:
console.log(this.list.find(el => el.id === id))