set and get array inside cookie in javascript

Solutions on MaxInterview for set and get array inside cookie in javascript by the best coders in the world

showing results for - "set and get array inside cookie in javascript"
Isaac
16 Jan 2019
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);
Delbert
06 Jan 2021
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');
queries leading to this page
javascript array of cookesarray to document cookiedocument cookie arrayjavascript cookie array valuejavascript set cookie array valueparse cookies and store in arrayset cookie as arraycookies array javascriptarray to cookie javascript objecthow to set array in cookie in javascriptjs set array cookieget cookie array javascriptjs get cookie arraystore cookie values in array using javscriptcookie set array javascriptset cookie to array jsstore cookie values in array using jsset cookie value array javascriptcookie array javascriptjavascript cookies arraypush array in cookiejavascript cookie arrayres cookie not working with array javascriptjs set cookie arrayjs store array in cookiejavascript get cookie as arrayjs combine all cookies int oan arraystore an array in cookie javascriptcookie js return arrayset and get array inside cookie in javascriptput all cookies in array javascriptarray cookie javascriptarray in cookie javascriptjavascript get cookie arrayget array in cookie jsset array in cookie by jshow to get an array from a cookie 3fjavascript add array to cookieupdate cookie where array same cookie javascript array valueset cookie javascript arrayarray to cookies jsimplemet javascrip cookie in a list or arraystore array in session or cookie javascriptget cookie javascript array valuejs array to cookie stringuse array inside cookie javascriptget and set array iniside cookie in javascriptget cookies in array jsadd array to cookieset and get array inside cookie in javascript