0

I had % in my cookie and I found following code for it and got the data below after implying that code

var cookies = (document.cookie);
var output = {};
cookies.split(/\s*;\s*/).forEach(function (pair) {
    pair = pair.split(/\s*=\s*/);
    var name = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair.splice(1).join('='));
    output[name] = value;
});
console.log(output);

The data console is down below;

{"objName":"[{"key":1,"key2":"value 123","key3":"value123"},{"key":1,"key2":"value 123","key3":"value123"}]"}

I have the data as shown above, What I want is to objName into array and remove "" from in front of [] array barckets

objName=[{"key":1,"key2":"value 123","key3":"value123"},{"key":1,"key2":"value 123","key3":"value123"}]
sumitkhatrii
  • 116
  • 2
  • 5
  • 20

1 Answers1

0

As far as I understand, you are trying to get cookie values, you can try this code and handle the returned array as you need. You can try this solution and let me know if it works.

var cookies = document.cookie.split(';').reduce(
    (cookies, cookie) => {
      const [name, val] = cookie.split('=').map(c => c.trim());
      cookies[name] = val;
      return cookies;
    }, {});
console.log(cookies);
DreamBold
  • 2,727
  • 1
  • 9
  • 24
  • This doesnot work as required as it returned cookie value as it is in this way cookievalue =cookievalue%cookievaue%. This might need to be json parse or stringify to get desired result – sumitkhatrii Nov 28 '22 at 18:27
  • The second one , didn't try the first one though. @DreamBold – sumitkhatrii Nov 28 '22 at 18:48
  • You can try the first one and let me know. @sumitkhatrii So I can update the answer accordingly. :) – DreamBold Nov 28 '22 at 18:49