Search code examples
javascriptreactjsreact-nativeasyncstorage

How to get multi keys from AsyncStorage and then add these keys into an array?


I wanna get multi keys from AsyncStorage and add these keys into an array.

AsyncStorage.multiGet(
 ['key1',
  'key2',
  'key3',
  'key4',
  'key5',]
).then(() => {

})

Solution

  • You can use map function for this:

    AsyncStorage.getAllKeys((err, keys) => {
      AsyncStorage.multiGet(keys, (err, stores) => {
        stores.map((result, i, store) => {
          // get at each store's key/value so you can work with it
          let key = store[i][0];
          let value = store[i][1];
        });
      });
    });
    

    this is an example from doc here AsyncStorage