Solution:
Pictorial Represantation:
Step 1) Use String.split(';') to separate key-value pairs from each other.
Step 2) Use Array.map() and String.split('=') to separate keys from values in each pair.
Step 3) Use Array.reduce() and decodeURIComponent() to create an object with key-value pairs.
Example:
const parseCookie = string =>
string.split(';').map(item => item.split('=')).reduce((k, i) => {
k[decodeURIComponent(i[0].trim())] = decodeURIComponent(i[1].trim());
return k;
}, {});
testInput: console.log(parseCookie('jshub=great; javascript=js; math=95'));
output: { jshub: 'great', javascript: 'js', math: '95' }
0 Comments
Thank you for Comment