1

My First Object is

conversation = {
  "members": [
    "613aa457e8d5f249922e7f86",
    "613aa457e8d5f249922e7faa"
  ],
  "_id": "613aace085dee060fdeb6a9c",
}

My Second object is

message = {
  "read": false,
  "_id": "613aadd58becf96394001efa",
  "sender": "613aa457e8d5f249922e7f86",
  "text": "hi",
  "conversation_id": "613aadcc8becf96394001ee8",
}

My Expected Result is

{
  "members": [
    "613aa457e8d5f249922e7f86",
    "613aa457e8d5f249922e7faa"
  ],
  "_id": "613aace085dee060fdeb6a9c",
  "message": {
    "read": false,
    "_id": "613aadd58becf96394001efa",
    "sender": "613aa457e8d5f249922e7f86",
    "text": "hi",
    "conversation_id": "613aadcc8becf96394001ee8",
  }
}

At First I tried with spread operator by following this link

let new_object= { ...conversation, messages }

and i got the result something like this

enter image description here

I also tried with these way by following stack-overflow

let new_object= Object.assign(conversation, message);

let new_object= Object.assign({},conversation, message);

But I don't get my expected result.

5
  • 1
    it looks like your converstation and message objects are completely different to what you say they are Commented Sep 10, 2021 at 3:25
  • 1
    The spread operator would do. codepen Commented Sep 10, 2021 at 3:25
  • 2
    There is no need to spread messages @JuanMendes. Because OP wants the message to be an individual object. Commented Sep 10, 2021 at 3:28
  • @HR01M8055 It works in this way, but i get this conversation and message object from database, and when i call that from API , i couldn't get what i want Commented Sep 10, 2021 at 3:36
  • let new_object= { ...conversation, messages } does work, but if you're working with data you're getting asynchronously you may have to await for all the data to be returned before you can merge them together. Commented Sep 10, 2021 at 3:46

1 Answer 1

2

It looks like you might be loading your data from mongoose, which by default is going to decorate your objects such that they aren't the "plain old javascript objects" you think they are.

Mongoose provides a toObject method on Document that can convert an object you loaded from the DB to the plain object you're expecting. You probably want to do something like this:

  // assume conversationFromDb and messageFromDb are Mongoose documents
  const conversation = conversationFromDb.toObject();
  const message = messageFromDb.toObject();

  const newObject = { ...conversation, message };
Sign up to request clarification or add additional context in comments.

2 Comments

yes mate, i am loading all of these data from mongoDB database, i am going to try this way and let you know
thank you brother, i got my solution. But now i have done it implementing mongodb aggregation framework

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.