簡體   English   中英

無法通過使用method(passing)索引作為參數從Array檢索數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM