1

I have a top level JSON structure as follows:

{
    "video": [],
    "messages": [],
    "notifications": []
}

and i have a database output (in variable "result") as follows i want to push into the "video" array:

[
    {
        "_id": "5f98ab906439155cfc6f9afb",
        "status": "NOT_STARTED",
        "date": "2020-10-27T23:21:52.683Z",
        "callInvitees": [
            {
                "username": "user1"
            },
            {
                "username": "user2"
            }
        ]
    },
    {
        "_id": "5f98aba0789e163e0c78908f",
        "status": "NOT_STARTED",
        "date": "2020-10-27T23:22:08.048Z",
        "callInvitees": [
            {
                "username": "user1"
            }
        ]
    }
]

My code is:

let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(result)

It works but i am ending up with too many arrays (i think) - it looks as follows:

{
    "video": [
        [
            {
                "_id": "5f98ab906439155cfc6f9afb",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:21:52.683Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    },
                    {
                        "username": "user2"
                    }
                ]
            },
            {
                "_id": "5f98aba0789e163e0c78908f",
                "status": "NOT_STARTED",
                "date": "2020-10-27T23:22:08.048Z",
                "callInvitees": [
                    {
                        "username": "user1"
                    }
                ]
            }
        ]
    ],
    "messages": [],
    "notifications": []
}

I want "video": [ { ... }, { ... } ] whereas i have "video": [[ { ... }, { ... } ]]

How can i resolve this?

2 Answers 2

1

You can use the spread operator as follows:

let result = [
    {
        "_id": "5f98ab906439155cfc6f9afb",
        "status": "NOT_STARTED",
        "date": "2020-10-27T23:21:52.683Z",
        "callInvitees": [
            {
                "username": "user1"
            },
            {
                "username": "user2"
            }
        ]
    },
    {
        "_id": "5f98aba0789e163e0c78908f",
        "status": "NOT_STARTED",
        "date": "2020-10-27T23:22:08.048Z",
        "callInvitees": [
            {
                "username": "user1"
            }
        ]
    }
]

let dashboardJSON = { "video": [], "messages": [], "notifications": [] };

dashboardJSON.video.push(...result)

console.log(dashboardJSON);

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

2 Comments

Amazing, thankyou! What is the "spread operator" doing?
@Kevin you are welcome. It's expanding the items of result to be pushed individually instead of a list of objects.
0

Just use Array.prototype.map() method. Map method returns a new array with the result by using the provided function.

const ret = {
  video: [],
  messages: [],
  notifications: [],
};

const data = [
  {
    _id: '5f98ab906439155cfc6f9afb',
    status: 'NOT_STARTED',
    date: '2020-10-27T23:21:52.683Z',
    callInvitees: [
      {
        username: 'user1',
      },
      {
        username: 'user2',
      },
    ],
  },
  {
    _id: '5f98aba0789e163e0c78908f',
    status: 'NOT_STARTED',
    date: '2020-10-27T23:22:08.048Z',
    callInvitees: [
      {
        username: 'user1',
      },
    ],
  },
];

ret.video = data.map((x) => x);
console.log(ret);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.