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:
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)