javascript - 从 AsyncStorage 中删除特定项目

标签 javascript react-native asyncstorage

我知道可以像这样从 asyncStorage 中删除整个 key AsyncStorage.removeItem('users'); 但如何从用户 key 中删除特定项目,例如从用户 key 中删除 id 2:

[{id:1, user:A}, {id:2, user:B}, {id:3, user:C}]

最佳答案

使用 AsyncStorage.setItem('users', someOtherObject) 将覆盖存储中的现有对象。因此,只需阅读、修改并写回即可。

const users = await AsyncStorage.getItem('users');
const alteredUsers = users.splice(users.findIndex(x => x.id === someId), 1);
AsyncStorage.setItem('users', alteredUsers);

关于javascript - 从 AsyncStorage 中删除特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001895/

相关文章:

javascript - Twitter Bootstrap : How to close modal dialog?

javascript - 如何根据用户输入使用 Node.js 发出 HTTP 请求?

javascript - React Native AsyncStorage getItem 返回 promise 而不是值(value)

javascript - 使用 Jest-expo 测试异步存储的示例用例?

authentication - 无法从 React Native 中的 AsyncStorage 返回 token

javascript - 使用 jQuery 在 WooCommerce 上增加/减少数量

javascript - 为什么 context.clearRect() 在 requestAnimationFrame 循环中不起作用?

Android独有的闪屏: how to make it fill screen?

reactjs - react native 事件