fazal abbas
fazal abbas

Reputation: 37

how to add data in cookie array without push into array jquery

I want to add the object value to cookie array without push array to cookie. how can i do that ?i can use cookieDevObjArray.push(obj); but i want to push my object to cookie not whole array.How can i do that any help? Here is object

 var obj =
 {
    'deviceid': deviceId,
    'full': cookiefull,
    'stacked': istoggle

 };

 $.cookie("cookieDevObjArray", + JSON.stringify(obj), { expires: 30 });

this code only save current object and override previous data.

Upvotes: 0

Views: 190

Answers (1)

Momo
Momo

Reputation: 960

You should first get your specific cookie key then parse it and doing your operation then set the cookie again

 var obj =
 {
    'deviceid': deviceId,
    'full': cookiefull,
    'stacked': istoggle

 };

// First get your previous cookie
const array = JSON.parse($.cookie("cookieDevObjArray"))
array.push(obj)

// Setting new value
$.cookie("cookieDevObjArray", + JSON.stringify(array), { expires: 30 });

Upvotes: 1

Related Questions