I'm not human
I'm not human

Reputation: 554

Remove specific item from AsyncStorage

I know it is possible to remove a whole key from asyncStorage like this AsyncStorage.removeItem('users'); but how can I remove a specific item from inside the users key, for example remove id 2 from users key:

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

Upvotes: 1

Views: 798

Answers (1)

Kraylog
Kraylog

Reputation: 7553

Using AsyncStorage.setItem('users', someOtherObject) will override the existing object in the store. So, just read, alter and write back.

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

Upvotes: 4

Related Questions