简体   繁体   中英

Javascript - How to get rid of all square brackets in a Array

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!

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])

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM