[英]Cant retrieve the data from the Array by using method(passing) index as argument
在將index作為參數顯示為undefined時無法使用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])
}
您的數組包含對象,但是您想按id
搜索,這些id
是這些對象的屬性。 您必須使用filter
:
console.log(this.list.find(el => el.id === id))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.