Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
How can I reconstruct raw headers from request into an array?

I am trying to reconstruct the raw headers from the request object from looking like this:

[{"0": "Host"},
 {"1": "localhost:3000"},
 {"2": "Connection"},
 {"3": "keep-alive"}, 
{"4": "sec-ch-ua"}, 
{"5": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\""}]

to something like:

[{ "Host: "localhost:3000"}, {"Connection": "Keep alive"}]

is this possible in javascript?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can first flatMap() them to an array of values and then reduce() them pairwise to your desired result object:

const headers = [
  {"0": "Host"},
  {"1": "localhost:3000"},
  {"2": "Connection"},
  {"3": "keep-alive"}, 
  {"4": "sec-ch-ua"}, 
  {"5": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"102\", \"Google Chrome\";v=\"102\""}
];

const result = headers.flatMap(Object.values).reduce((r, _, i, a) => 
  i % 2 ? r : {
    ...r,
    [a[i]]: a[i + 1]
  }, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!