1

I want to create a shopping cart. I use this code to save some data like username. Now I want to save product ID and Count in an array in AsyncStorage. (I use expo.)

How can I do that?

  _save_user = async () => {
  await SecureStore.setItemAsync('username', parseInt(this.state.Username).toString());
  };

Please tell me if you have some better idea.

1 Answer 1

3

you can try something like this:

var item1 = {itemCode:"10101010", quantity:"500", anythingElse:"this"};
var item2 = {itemCode:"10102020", quantity:"42", anythingElse:"is"};
var item3 = {itemCode:"10103030", quantity:"2", anythingElse:"something"};
var yourArray = [item1,item2,item3];

  _save_array = async () => {
     await SecureStore.setItemAsync('yourKey', JSON.stringify(yourArray));
  };


Sign up to request clarification or add additional context in comments.

2 Comments

This way is not good. How can I save count of a product? And after that I want to show product and count in one page like cart page. How can I get that array? encode data to json is so good, but how can I save data like this: [username]=>{[productName]=> "Samsung" , [count]=> "5", [sum]=> "25800"}
@AlirezaDarvishi I just modify it, you can acces that object normally like yourArray[<ItemPosition>].itemCode / yourArray[<ItemPosition>].quantity / yourArray[<ItemPosition>].anythingElse ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.