gpt4 book ai didi

javascript - 从本地存储过滤数据

转载 作者:行者123 更新时间:2023-11-28 18:08:03 24 4
gpt4 key购买 nike

我在本地存储中存储了很少的数据作为对象数组,例如

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

我想在文本框中显示登录用户的数据。当我编写 localStorage.getItem('key_users') 时,它向我显示所有用户的详细信息,但如何获取特定索引的用户的详细信息?

最佳答案

作为函数

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

getUserById(0)

关于javascript - 从本地存储过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com