2
  *// Creating An Array
    const someArray = [1,2,3,4];
    return AsyncStorage.setItem('somekey', JSON.stringify(someArray))
    .then(json => console.log('success!'))
     .catch(error => console.log('error!'));



   //Reading An Array 
   return AsyncStorage.getItem('somekey')
  .then(req => JSON.parse(req))
  .then(json => console.log(json))
  .catch(error => console.log('error!'));
   * 

How Can I Update a Specific Index Of Array And Delete an index
e.g, new Array should be {1,A,3}

Abdul Manan
  • 43
  • 1
  • 9

1 Answers1

3

With AsyncStorage, it's best if you treat the data structure as immutable. So basically, to perform an update, you grab what you want, mess with it however, and put it back under the same key.

return AsyncStorage.getItem('somekey')
  .then(req => JSON.parse(req))
  .then(json => {
          const temp = json;
          temp[2] = 'A';
          temp.pop(); // here it's [1, A, 2]
          AsyncStorage.setItem('somekey', JSON.stringify(temp));
       })
  .catch(error => console.log('error!'));

And then to remove any item, just do AsyncStorage.removeItem('somekey'). There are no direct operations with AsyncStorage to let you do deeper updates, just a key/value data sets.

Predrag Beocanin
  • 1,402
  • 3
  • 18
  • 25
  • Can I Perform CRUD Operation Using Realm or any other database.If you have such example please share it. It will help me with my assignment. I Need Only Simple Array CRUD operation...... – Abdul Manan Jan 31 '19 at 14:17
  • There are implementations where people use AsyncStorage, and a service manages that in the cloud basically, and Realm seems like one of them. If you want a full scale backend at your disposal, check out Firebase - it's not that hard to slap into an application. – Predrag Beocanin Jan 31 '19 at 14:19
  • Please check this answer for some data storage options in react native https://stackoverflow.com/questions/44376002/what-are-my-options-for-storing-data-when-using-react-native-ios-and-android – Predrag Beocanin Jan 31 '19 at 14:19