((javascript)本地存储值索引

问题描述 投票:1回答:2

我将数据设置为本地存储中名为“ todo”的键。

结构是这样。

key: todo
value : [{"text":"text1","idx":1},
        {"text":"text2","idx":2},
        {"text":"text4","idx":4}]

如何找到idx =“ 4”的对象的索引?

例如,idx = 1的对象的索引为0。

key: todo
value : [{"text":"text1","idx":1} => index: 0
        {"text":"text2","idx":2} => index: 1
        {"text":"text4","idx":4}] => index: 2
javascript local-storage indexof
2个回答
1
投票

使用todo.findIndex(elem => elem.idx === 4 )


1
投票

假设您已经从本地存储中解析了JSON字符串(如果不能,则可以使用JSON.parse(),可以使用.findIndex()获取具有给定id的对象的索引:

.findIndex()
© www.soinside.com 2019 - 2024. All rights reserved.