0

I have an API call in which i want merge two objects and return them

  router.get('/reviewsavg', async (req, res) => {
    try {
      //enitityId[] is an array consisting of ID's to look up form
      const promise = [];
      Object.keys(entityId).forEach(async (key) => {

        const reviews = Review.aggregate([
          { $match: { entityId: ObjectId(entityId[key]) } },
          {
            $group: {
              avg: { $avg: '$rating' },
            },
          },
        ]);

        const entit = Entity.find({ _id: entityId[key] });
        promise.push(entit, reviews);
      });
      const results = await Promise.all(promise);
      res.send(results);
    } catch (e) {
      res.status(500).send();
    }
  });

in this reviews look like

[
    [
        {
            "_id": null,
            "avg": 5
        }
    ],
    [
        {
            "_id": null,
            "avg": 3.66666
        }
    ]
]

and entit looks like this

[
   [
        {
            "public": false,
            "name": "second",
            "createdAt": "2020-05-09T16:28:38.493Z",
            "updatedAt": "2020-05-09T16:28:38.493Z",
            "__v": 0
        }
   ],
   [
       {
            "public": false,
            "name": "first",
            "createdAt": "2020-06-09T16:28:38.493Z",
            "updatedAt": "2020-06-09T16:28:38.493Z",
            "__v": 0
        }
   ]     
]

I want to merge these two send them back, I want the response like this

[
       {
            "public": false,
            "name": "first",
            "createdAt": "2020-06-09T16:28:38.493Z",
            "updatedAt": "2020-06-09T16:28:38.493Z",
            "__v": 0,
             "_id": null,
            "avg": 3.66666
        }
]

Till now I have tried

1.

const merged = [...entit, ...reviews];
      promise.push(merged);

returns []

2.

const merged =entit.concat(reviews);
      promise.push(merged);

returns error that entit.concat is not a function.

3. Tried Object.assign() that too doesn't work

What else can I do here?

Devang Mukherjee
  • 185
  • 1
  • 1
  • 11

2 Answers2

0
const combined = {}
const reviews = [
    [
        {
            "_id": null,
            "avg": 5
        }
    ],
    [
        {
            "_id": null,
            "avg": 3.66666
        }
    ]
]
const entit = [
   [
        {
            "public": false,
            "name": "second",
            "createdAt": "2020-05-09T16:28:38.493Z",
            "updatedAt": "2020-05-09T16:28:38.493Z",
            "__v": 0
        }
   ],
   [
       {
            "public": false,
            "name": "first",
            "createdAt": "2020-06-09T16:28:38.493Z",
            "updatedAt": "2020-06-09T16:28:38.493Z",
            "__v": 0
        }
   ]     
]
for (let i = 0; i < reviews.length; i++) Object.assign(combined, reviews[i][0]);
for (let i = 0; i < entit.length; i++) Object.assign(combined, entit[i][0]);
console.log(combined)
0

You're using destructuring on the 2 arrays. What you want to do is merging the properties of the objects inside the array. This should do the job:

merged = []
for (var i = 0; i < reviews.length; i++) {
  let elem = {}
  Object.assign(elem, reviews[i][0])
  Object.assign(elem,  entit[i][0]);
  merged.push(elem)
}

logging merged:

[
  {
    _id: null,
    avg: 5,
    public: false,
    name: 'second',
    createdAt: '2020-05-09T16:28:38.493Z',
    updatedAt: '2020-05-09T16:28:38.493Z',
    __v: 0
  },
  {
    _id: null,
    avg: 3.66666,
    public: false,
    name: 'first',
    createdAt: '2020-06-09T16:28:38.493Z',
    updatedAt: '2020-06-09T16:28:38.493Z',
    __v: 0
  }
]
trolloldem
  • 669
  • 4
  • 9