2017-09-05 68 views
2

這是我如何將數據集存儲如何從數組中獲取特定值?

this.storage.set('userData', JSON.stringify(this.responseData)) 


的數據看起來是這樣,當保存到IndexedDB的

[{"user_id":1,"username":"admin"...}] 

的問題是,如果我提取陣列只需要1字符

this.storage.get('userData').then((res) => { console.log(res) 
    console.log(res[3]) 
}); 

像這個代碼上面。該res[3]只採信u
我想取的名字和喜歡,例如,如果我稱之爲res.user_id它會給我1res.usernameadmin

回答

2

既然你做了JSON.stringify值...數據現在存儲爲一個字符串..所以你現在正在取得字符串「[{\」user ...「的第四個字母,即你得到的」u「。 只需製作一個JSON.parse(res )先......然後你應該得到想要的結果

this.storage.get('userData').then((res) => { 
console.log(res) 
var users =JSON.parse(res) 
console.log(users[0].username) 
});