1JSON encode it, effectively producing a string like "{name:'myname',age:'myage'}"
2which you put in a cookie, retrieve when needed and decode back into a
3JavaScript array/object.
4
5Example - store array in a cookie:
6----------------------------------
7var arr = ['foo', 'bar', 'baz'];
8var json_str = JSON.stringify(arr);
9createCookie('mycookie', json_str);
10
11Later on, to retrieve the cookie's contents as an array:
12--------------------------------------------------------
13var json_str = getCookie('mycookie');
14var arr = JSON.parse(json_str);
1cookie = {
2 set: function(name, value) {
3 document.cookie = name+"="+value;
4 },
5 get: function(name) {
6 cookies = document.cookie;
7 r = cookies.split(';').reduce(function(acc, item){
8 let c = item.split('='); //'nome=Marcelo' transform in Array[0] = 'nome', Array[1] = 'Marcelo'
9 c[0] = c[0].replace(' ', ''); //remove white space from key cookie
10 acc[c[0]] = c[1]; //acc == accumulator, he accomulates all data, on ends, return to r variable
11 return acc; //here do not return to r variable, here return to accumulator
12 },[]);
13 }
14};
15cookie.set('nome', 'Marcelo');
16cookie.get('nome');