Suman
Suman

Reputation: 93

how to get the response from array of objects

I have a below data and i want to show only few fields _id, courseBy in my response

const result =  [{
_id: 6104106f3ff78f39b81bf143,
category: 't-shirts',
courseBy: ' shivangi joshi',
thumbnail: 'uploads\\shirt.jpg',
about: ' shirts',
},
{
 _id: 610410763ff78f39b81bf147,
 category: 'watches',
 courseBy: ' shivangi joshi',
 thumbnail: 'uploads\\shirt.jpg',
 about: ' shirts',
}] 

Response data:-

 res.status(200).send({
   statusCode: "200",
     "_id":result[0]._id,
     "courseBy": reslut[0]._courseBy
});

here i'm only getting single record but not the both the records please help me thanks in advance

Upvotes: 0

Views: 35

Answers (1)

Manish Jangir
Manish Jangir

Reputation: 5437

res.status(200).send({
    statusCode: "200",
    data: result.map(({ _id, courseBy }) => ({ _id, courseBy }))
 });

Upvotes: 2

Related Questions