Search code examples
androidarraysobjective-creact-nativeasyncstorage

AsyncStorage returns array to string react-native


I have an array that I stored on AsyncStorage as Json.stringfy. when I try to get the key it returns as a string. what I want is like this [0:'apple', 1:'ball', 2:'car']. following is the method I have tried

  useEffect(() => {
     AsyncStorage.getItem('@moduke').then(module => {
        const a = JSON.parse(module);
        console.log(a.length);
     });
  }, []);

Solution

  • the String is surrounded by " ". and use " . when try to parse the above string by JSON.parse(), still return the string:{"0:'apple', 1:'ball', 2:'car'}, and \" is replaced by ". But use the JSON.parse() again, it will return the object i solved as below method

         useEffect(() => {
             AsyncStorage.getItem('@moduke').then(module => {
                const a = JSON.parse(module);
                const b = JSON.parse(a);
                console.log(b);
                console.log(b.length);
             });
          }, []);