Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How t convert JavaScript objects into array of objects?

I want to convert objects as shown here to array of objects:

My code:

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

I want to convert this to array of objects so, the output should look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I need the data like this in order to render it in HTML, so How can I do this?

>Solution :

How about this?

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

let objectToArray = [{ ...myObject
}]

console.log(objectToArray)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading