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

191
Views
How to rearrange array of objects into object of objects in node js?

I have Java script array like this:

arr =   [
            {
                "email": "info@gmail.com",
                "phoneNumber": "++255 638-1527",
            },
            {
                "email": "@gmail.com",
                "phoneNumber": "+255 532-1587",
            },
            {
                "email": "@gmail.com",
                "phoneNumber": "+255 613-1587",
                "info": [
                    {
                        "date": "2022-02-19",
                        "count": 1
                    },
                    {
                        "date": "2022-03-17",
                        "count": 9
                    },
                    {
                        "date": "2021-02-10",
                        "count": 10
                    }]
           }
    ]

I need to convert above array of objects arr into object with key-value pair like below

arr = 

        {
            "+255 638-1527":{
                "email": "info@gmail.com",
                "phoneNumber": "+255 638-1527",
            },
            "+255 532-1587":{
                "email": "@gmail.com",
                "phoneNumber": "+255 532-1587",
            },
            "+255 613-1587":{
                "email": "@gmail.com",
                "phoneNumber": "+255 613-1587",
                "info": [
                    {
                        "date": "2022-02-19",
                        "count": 1
                    },
                    {
                        "date": "2022-03-17",
                        "count": 9
                    },
                    {
                        "date": "2021-02-10",
                        "count": 10
                    }]
           }

I need the it like this JSON, in the form of key-value pair. How can I achieve this?

I need the data like this in order to render the output, can someone please help me with this?

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

0

Use Object.fromEntries like this:

const arr = [{"email": "info@gmail.com","phoneNumber": "++255 638-1527",},{"email": "@gmail.com","phoneNumber": "+255 532-1587",},{"email": "@gmail.com","phoneNumber": "+255 613-1587","info": [{"date": "2022-02-19","count": 1},{"date": "2022-03-17","count": 9},{"date": "2021-02-10","count": 10}]}];
    
const result = Object.fromEntries(arr.map(item =>
    [item.phoneNumber, item]
));

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!