I know there are a bunch of questions out there regarding this, but I have tried several different proposed solutions and get the same results for each one.
I am storing checkbox values in an array in the format timeArr = [ event-name-one: 0: 1404915600, 1: 1404917400 ]
. When the array gets updated with a new key/value pair a cookie is updated which stores the array as a JSON object.
I am storing the JSON object in the cookie using jQuery.cookie('day', JSON.stringify(timeArr), {expires: 7});
which stores the array in the following format (returned from console.log();
):
{"event-name-one":{"0":1405346400,"1":1405347600},"event-name-two":{"0":1405357200,"1":1405358400}}
In this instance event-name-one
and event-name-two
are the ID's of the checkboxes. I need to loop through the returned cookie value (JSON object) and check the checkboxes whos ID's are found in the returned cookie.
I have tried a few different loops, ie for(var k in cookieValue){}
and jQuery.each(jQuery.parseJSON(cookieValue), function(i, v) {});
with no luck.
The for(var k in cookieValue)
loop returns each letter of the object separately and the jQuery.each()
loop returns this error: Cannot use 'in' operator to search for '76' in ...
How can I convert this JSON string back to an array so I can loop through it and get the 'keys'
jQuery.cookie()
must stringify arrays automatically into JSON objects because in order to get the correct results from the loop I had to use jQuery.parseJSON()
on the response twice.
Example:
var cookieVal = jQuery.cookie('day_planner');
jQuery.each(jQuery.parseJSON(jQuery.parseJSON(cookieVal)), function(i, v) {
console.log(i, v);
});
This loop returns event-name-one Object {0: "1405346400", 1: "1405347600"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.