I have an image gallery and I am storing the image names into localstorage using:
var user_selected_images = JSON.stringify(output);
localStorage.setItem('selectedFiles', user_selected_images);
When a image from the gallery is clicked,I want to check if the is image is present in the array.
var selFiles = localStorage.getItem('selectedFiles');
var selFiles = JSON.parse(selFiles);
When I try to assess the typeof variable
console.log(typeof selFiles)
I get an output as string. Currently my getItem output looks like this:
["STAR_SPORTS-00001.jpg","STAR_SPORTS-00002.jpg"]
I tried using jQuery.makeArray(selFiles) and various options that I could find in SO, but still my getItem remains as string and not as array.
var output = ["STAR_SPORTS-00001.jpg","STAR_SPORTS-00002.jpg"]; user_selected_images = JSON.stringify(output); console.log(user_selected_images); var selFiles = JSON.parse(user_selected_images); console.log(typeof selFiles,selFiles)Can you console.log something and perhaps there is an error somewhereoutputis already a strifigied JSON. Can you try not to stringify and store it as it is.outputis already a JSON string... so this line is unnecessaryJSON.stringify(output);............. trylocalStorage.setItem('selectedFiles', output);console.log(typeof output)