简体   繁体   中英

Filter data from local storage

I have few datas stored in local storage as array of objects, like

[{"id":0,"firstname":"test","lastname":"test","email":"test@test.com"},
{"id":1,"firstname":"demo","lastname":"demo","email":"demo@demo.com"}];

I want to display the data of user logged in, in the text box. When I am writing localStorage.getItem('key_users'), its showing me details of all users, but how to get detail of a user of a particular index?

As a function

function getUserById(id) {
    return JSON.parse(localStorage.getItem('users')).filter(users => users.id === id)
}

getUserById(0)

Easy: deserialize local storage's item using JSON.parse :

var users = JSON.parse(localStorage.getItem('key_users'));
var user0 = users[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM