Here is an example of an array:
"data": [
{
"photo": "no-photo.jpg",
"_id": "5e9aabd9c975a10a7ee48476",
"title": "Title",
"description": "Description",
"phone": "77477926783",
"fromLocation": "5e9aa8a9c975a10a7ee48474",
"toLocation": "5e9aa8bac975a10a7ee48475",
"price": 1000000,
"date": "1998-02-23T00:00:00.000Z",
"user": "5e91729f147813258ef1f373",
"createdAt": "2020-04-18T07:27:21.743Z",
"__v": 0,
"courses": [],
"id": "5e9aabd9c975a10a7ee48476"
}
]
When using for...loop
:
for (const item of res.advancedResults.data) {
console.log(item)
}
The output is standard:
{
"photo": "no-photo.jpg",
"_id": "5e9aabd9c975a10a7ee48476",
"title": "Title",
"description": "Description",
"phone": "77477926783",
"fromLocation": "5e9aa8a9c975a10a7ee48474",
"toLocation": "5e9aa8bac975a10a7ee48475",
"price": 1000000,
"date": "1998-02-23T00:00:00.000Z",
"user": "5e91729f147813258ef1f373",
"createdAt": "2020-04-18T07:27:21.743Z",
"__v": 0,
"courses": [],
"id": "5e9aabd9c975a10a7ee48476"
}
But when trying to copy it like this:
for (const item of res.advancedResults.data) {
console.log({...item})
}
The output becomes:
{ '$__':
InternalCache {...},
isNew: false,
errors: undefined,
_doc:
{ photo: 'no-photo.jpg',
... },
'$locals': {},
'$$populatedVirtuals': { courses: [] },
'$init': true }
How can one correctly duplicate the object?