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

115
Views
how to take a value from nested object in headers
  headers: {
    host: 'localhost:3000',
    connection: 'keep-alive',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',



Code: 

    const user: User = req['user'];
    return {
      userAgent: {
        agent: req.headers.connection.reduce((el) =>
          Object.fromEntries(
            Object.entries(el).map(([k, v]) => [k, Object.values(v)[0]]),
          ),
        ),
      },
      headers: req.headers,
    };
  }
}


I have to take user-agent from connection. 


 I have used the map and reduce function but its says map cannot be used for string. 

If i am doing req.headers.connection i am getting only "keep-alive"

i have also used. req.headers.connection[6], but didn't get the desired result.

desired result is getting user-agent from connection

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

0

req.headers.connection is a single item. Maybe you're getting confused because the other keys of req.headers have quotes. This should work:

return {
  userAgent: {
    agent: req.headers["user-agent"]
  },
  headers: req.headers,
};
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!