I need to get all the cookies from the browser

前端 未结 9 1303
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 09:52

I need to get all the cookies stored in my browser using JavaScript. How can it be done?

9条回答
  •  渐次进展
    2020-12-04 10:44

    Added trim() to the key in object, and name it str, so it would be more clear that we are dealing with string here.

    export const getAllCookies = () => document.cookie.split(';').reduce((ac, str) => Object.assign(ac, {[str.split('=')[0].trim()]: str.split('=')[1]}), {});
    

提交回复
热议问题