0

Here are the browsers cookies:

console.log(document.cookie);
//=> name_instagram=true; name_googleplus=true;

And here is my code:

console.log(document.cookie.split(';').filter( (c) => c.startsWith(parts[1])).map( (d) => d.replace(/.*_(.*)=true/, "$1")));
//=> ["instagram"]

While the expected result is ["instagram", googleplus]. How can I get that?


Currently as you see, just the last item in the cookie will be stored into the array as result value. What's wrong?

3
  • What's parts[1]? "name_"? Commented Oct 10, 2017 at 12:26
  • @Walk Yes, it's one item of an array which is name. i.e var parts = ['id', 'name']; Commented Oct 10, 2017 at 12:27
  • After your startsWith filter second item in array will be " name_googleplus=true;", note leading space, so it starts with " name_" instead of "name_". Commented Oct 10, 2017 at 12:33

1 Answer 1

1

After your startsWith filter second item in array will be " name_googleplus=true;", note leading space, so it starts with " name_" instead of "name_". One way to fix it is using trim() to remove whitespaces.

console.log(document.cookie.split(';').filter( (c) => c.trim().startsWith('name_')).map( (d) => d.replace(/.*_(.*)=true/, "$1")));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.