0

I'm attempting to create a 'document bank' where the user can save documents and then download as a batch of files. The Cookies seems to be saving the array values, but I'm having trouble figuring out how to return the length of the stored array as well as how to loop through all of the values stored within it.

Here's what I have going on so far:

  $('.btn-save').click(function(e){
    e.preventDefault();
    counter++;
    $('.doc-counter').text(counter);

    var docVal = $(this).data('url');
    docArray.push(docVal);

    Cookies.set('docBank', docArray, {
      expires: 1
    });
    Cookies.set('docCount', $('.doc-counter').text(counter), {
      expires: 1
    });

  });

  console.log(Cookies.get('docBank'));

What I'm trying to achieve is:

  • Store all data attributes (file URLs) into an array
  • Store all values in the array using Cookies
  • Get count of stored values in array
  • Loop through values of stored array and eventually spit out file downloads

console.log(Cookies.get('docBank').length); doesn't seem to be doing the trick for returning the total array size.

Any guidance on this would be appreciated! I'm pretty sure there's a simple solution to this that I'm not seeing.

Thanks!

Armin Šupuk
  • 809
  • 1
  • 9
  • 19
m918273
  • 110
  • 2
  • 11

1 Answers1

1

You have to use the Cookies.getJSON method to get the array, otherwise you will get only the array as a JSON string.

console.log(Cookies.getJSON('docBank').length);
Armin Šupuk
  • 809
  • 1
  • 9
  • 19