如何从 AsyncStorage 获取多个键,然后将这些键添加到数组中?

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

我想从 AsyncStorage 获取多个键并将这些键添加到数组中。

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

})

您可以为此使用地图功能:

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

这是文档中的示例 AsyncStorage

async getKeysData(keys){
  const stores = await AsyncStorage.multiGet(keys);
  return stores.map(([key, value]) => ({[key]: value}))
}

getKeysData(['key1', 'key2', 'key3'])
 .then((response)=>{ console.log(response)})
 
 /*
 Respose will be in below form 
 response = [
  {key1: 'DATAOF key1'},
  {key2: {"DATA OF KEY2"}}
  {key3: 'DATAOF key1'}
*/