-1

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

My code:

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
                }]
       }
}

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

entireObject =   [
        {
            "email": "[email protected]",
            "phoneNumber": "++255 638-1527"
        },
            "email": "[email protected]",
            "phoneNumber": "+255 532-1587"
        },
        {
            "email": "[email protected]",
            "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?

7
  • 2
    Object.values(entireObject)? Commented Mar 7, 2022 at 21:06
  • @evolutionxbox No change, if I do that Commented Mar 7, 2022 at 21:10
  • 1
    No change? What do you mean? The code I suggested won't affect entireObject, but it does return the array you want. Put it in a new variable Commented Mar 7, 2022 at 21:11
  • yeah I have put it in a seperate varibale and console.log() it Commented Mar 7, 2022 at 21:26
  • 1
    NOTE: Your "desired output" is an invalid object as you have it now Commented Mar 7, 2022 at 21:48

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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)

1 Comment

This doesn't result in the desired output
-1

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.

3 Comments

Rather than a link answer you should vote to close as a duplicate
Mark Schultheiss, I found it interesting that you voted my attempt to direct the asker to the solution down. Anyways, it was my very first answer and I will try to do better next time.
Please do not assume votes here as I did not vote you up or down.
-1

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

1 Comment

Why use map here? It seems pointless

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.