Search code examples
javascriptreact-nativeasyncstorage

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}]

Solution

  • 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);