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": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9a0a7afa689aea4a8a0a5e7aaa6a4">[email protected]</a>",
            "phoneNumber": "++255 638-1527"
        },
        "+255 532-1587": {
            "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afdac6c7d6efc8c2cec6c381ccc0c2">[email protected]</a>",
            "phoneNumber": "+255 532-1587"
        },
        "+255 613-1587": {
            "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b707778735b7c767a727735787476">[email protected]</a>",
            "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:

entireObject =   [
        {
            "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4cdcac2cbe4c3c9c5cdc88ac7cbc9">[email protected]</a>",
            "phoneNumber": "++255 638-1527"
        },
            "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24514d4c5d644349454d480a474b49">[email protected]</a>",
            "phoneNumber": "+255 532-1587"
        },
        {
            "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741f18171c341319151d185a171b19">[email protected]</a>",
            "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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The desired result should end with ] in order to be a valid array, not }. All you need is Object.values() as in the following demo.

const entireObject =   {
        "++255 638-1527": {
            "email": "[email protected]",
            "phoneNumber": "++255 638-1527"
        },
        "+255 532-1587": {
            "email": "[email protected]",
            "phoneNumber": "+255 532-1587"
        },
        "+255 613-1587": {
            "email": "[email protected]",
            "phoneNumber": "+255 613-1587",
            "info": [
                {
                    "date": "2022-02-19",
                    "count": 1
                },
                {
                    "date": "2022-03-17",
                    "count": 9
                }]
       }
};

const arr = Object.values( entireObject );

console.log( arr );

Method 2

How about this?

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

let objectToArray = [{ ...myObject
}]

console.log(objectToArray)

Method 3

Just wanted to point you in the right direction as a similar question was asked and perfectly answered. Here’s the link: How to convert object containing Objects into array of objects
N.B. please make sure to see the answer which is voted as the best answer.

Method 4

you could do something like this:

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

let result = Object.values(myObject).map((item) => ({ ...item
}));
console.log(result);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x