Reputation: 309
I am trying to get header data from websites such as Google and its going pretty well. The problem is that there's a lot of square brackets that I need to get rid of since those brackets are causing some issues with how I am using them (I am setting those as the response headers in Node.js app).
{
date: [ 'Thu, 03 Sep 2020 03:45:32 GMT' ],
expires: [ '-1' ],
'cache-control': [ 'private, max-age=0' ],
'content-type': [ 'text/html; charset=UTF-8' ],
'strict-transport-security': [ 'max-age=31536000' ],
p3p: [ 'CP="This is not a P3P policy! See g.co/p3phelp for more info."' ],
'content-encoding': [ 'gzip' ],
server: [ 'gws' ],
'x-xss-protection': [ '0' ],
'set-cookie': [
'NID=204=U6hVPXuZiH-T-DjyvLXiq9L5i3xt5TfKvTA0hY0EgPeksXwFjezsQfVjatUfj909sP1hCdyea3HxiycPT9oCBwS7JSFI6c5LivCkZZ2zJddeV_mx05I14piRoBAsOJQKtOKeMU8onSaOntLIRFZ8qp2qM1mhj54djbua_5WH_3M; expires=Fri, 05-Mar-2021 03:45:32 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none'
],
'alt-svc': [
'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'
],
'transfer-encoding': [ 'chunked' ]
}
Should be
{
date: 'Thu, 03 Sep 2020 03:45:32 GMT' ,
expires: '-1' ,
'cache-control': 'private, max-age=0' ,
'content-type': 'text/html; charset=UTF-8' ,
'strict-transport-security': 'max-age=31536000' ,
p3p: 'CP="This is not a P3P policy! See g.co/p3phelp for more info."' ,
'content-encoding': 'gzip' ,
server: 'gws' ,
'x-xss-protection': '0' ,
'set-cookie':
'NID=204=U6hVPXuZiH-T-DjyvLXiq9L5i3xt5TfKvTA0hY0EgPeksXwFjezsQfVjatUfj909sP1hCdyea3HxiycPT9oCBwS7JSFI6c5LivCkZZ2zJddeV_mx05I14piRoBAsOJQKtOKeMU8onSaOntLIRFZ8qp2qM1mhj54djbua_5WH_3M; expires=Fri, 05-Mar-2021 03:45:32 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none'
,
'alt-svc':
'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'
,
'transfer-encoding': 'chunked'
}
I would really love the help!
Upvotes: 1
Views: 117
Reputation: 581
If the response is available to you as a JavaScript object, you can simply traverse through all the entries and set the entry value as a string value in place of an array(using for of). However, if the response is available to you in string format, string manipulation would be the key where you need to replace the first '[' and the last ']' in every line.
However, you need to keep a check on whether the original value is actually an array.
Upvotes: 0
Reputation: 370729
Map the object's entries to extract the first item from each, then turn it back into an object With Object.fromEntries
:
const output = Object.fromEntries(
Object.entries(input)
.map(([key, val]) => [key, val[0]])
);
const input = {
date: [ 'Thu, 03 Sep 2020 03:45:32 GMT' ],
expires: [ '-1' ],
'cache-control': [ 'private, max-age=0' ],
'content-type': [ 'text/html; charset=UTF-8' ],
'strict-transport-security': [ 'max-age=31536000' ],
p3p: [ 'CP="This is not a P3P policy! See g.co/p3phelp for more info."' ],
'content-encoding': [ 'gzip' ],
server: [ 'gws' ],
'x-xss-protection': [ '0' ],
'set-cookie': [
'NID=204=U6hVPXuZiH-T-DjyvLXiq9L5i3xt5TfKvTA0hY0EgPeksXwFjezsQfVjatUfj909sP1hCdyea3HxiycPT9oCBwS7JSFI6c5LivCkZZ2zJddeV_mx05I14piRoBAsOJQKtOKeMU8onSaOntLIRFZ8qp2qM1mhj54djbua_5WH_3M; expires=Fri, 05-Mar-2021 03:45:32 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none'
],
'alt-svc': [
'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'
],
'transfer-encoding': [ 'chunked' ]
};
const output = Object.fromEntries(
Object.entries(input)
.map(([key, val]) => [key, val[0]])
);
console.log(output);
If you aren't sure whether a value is an array first, then instead use
.map(([key, val]) => [key, Array.isArray(val) ? val[0] : val])
Upvotes: 3