0

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.

9
  • 4
    That sounds unlikely. I get an array if I do 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 somewhere Commented Feb 4, 2018 at 6:47
  • 1
    That can only happen if output is already a strifigied JSON. Can you try not to stringify and store it as it is. Commented Feb 4, 2018 at 6:48
  • Please update the question with the exact solution which you have. Commented Feb 4, 2018 at 6:49
  • 2
    Ithink output is already a JSON string... so this line is unnecessary JSON.stringify(output); ............. try localStorage.setItem('selectedFiles', output); Commented Feb 4, 2018 at 6:52
  • 1
    @PranavCBalan Better first do console.log(typeof output) Commented Feb 4, 2018 at 6:55

2 Answers 2

1

Are you sure your output variable is not a string? If I initialize output like this:

var output = ["STAR_SPORTS-00001.jpg","STAR_SPORTS-00002.jpg"];

console.log(typeof selFiles) returns object as expected.

Then I initialize output like this:

var output = '["STAR_SPORTS-00001.jpg","STAR_SPORTS-00002.jpg"]';

console.log(typeof selFiles) returns a string like the problem you are having.

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

Comments

1

That's not possible, it must return object not string. See the test below. Also use Array.isArray() to test it:

var output = ["STAR_SPORTS-00001.jpg", "STAR_SPORTS-00002.jpg"]
var user_selected_images = JSON.stringify(output);
var selFiles = user_selected_images;
var selFiles = JSON.parse(selFiles);
console.log(typeof selFiles);
console.log(Array.isArray(selFiles));

Perhaps your output is already a JSON string, in which case you shouldn't use JSON.stringify() on it. Save it directly to localStorage:

localStorage.setItem('selectedFiles', output);

Comments

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.