My json response structure:
[
{
"id": 14,
"groupname": "Angular",
"createdAt": "2017-12-15T15:06:39.000Z",
"updatedAt": "2017-12-15T15:06:39.000Z",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 20,
"gsm": "123456789"
}
},
{
"id": 2,
"contact": {
"id": 21,
"gsm": "987654321"
}
}
]
},
{
"id": 15,
"groupname": "React",
"createdAt": "2017-12-15T15:06:45.000Z",
"updatedAt": "2017-12-15T15:06:45.000Z",
"contactgroups": [
{
"id": 3,
"contact": {
"id": 21,
"gsm": "987654321"
}
}
]
},
{
"id": 16,
"groupname": "Vue",
"createdAt": "2017-12-15T15:06:51.000Z",
"updatedAt": "2017-12-15T15:06:51.000Z",
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"createdAt": "2017-12-17T16:07:38.000Z",
"updatedAt": "2017-12-17T16:07:38.000Z",
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"createdAt": "2017-12-21T05:50:50.000Z",
"updatedAt": "2017-12-21T05:50:50.000Z",
"contactgroups": []
}
]
i have two objects inside contactgroups array, so i need to return contactsCount as 2,
if i have one object inside contactgroups i need to return contactsCount as 1, is it possible to do with map method in Javascript?
I want final output like
[
{
"id": 14,
"groupname": "Angular",
"createdAt": "2017-12-15T15:06:39.000Z",
"updatedAt": "2017-12-15T15:06:39.000Z",
"contactsCount: 2,
"contactgroups": [
{
"id": 1,
"contact": {
"id": 20,
"gsm": "123456789"
}
},
{
"id": 2,
"contact": {
"id": 21,
"gsm": "987654321"
}
}
]
},
{
"id": 15,
"groupname": "React",
"createdAt": "2017-12-15T15:06:45.000Z",
"updatedAt": "2017-12-15T15:06:45.000Z",
"contactsCount: 1,
"contactgroups": [
{
"id": 3,
"contact": {
"id": 21,
"gsm": "987654321"
}
}
]
},
{
"id": 16,
"groupname": "Vue",
"createdAt": "2017-12-15T15:06:51.000Z",
"updatedAt": "2017-12-15T15:06:51.000Z",
"contactsCount: 0,
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"createdAt": "2017-12-17T16:07:38.000Z",
"updatedAt": "2017-12-17T16:07:38.000Z",
"contactsCount: 0,
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"createdAt": "2017-12-21T05:50:50.000Z",
"updatedAt": "2017-12-21T05:50:50.000Z",
"contactsCount: 0,
"contactgroups": []
}
]
see now i have contactsCount inside my final output.
This is my api code:
exports.getNewGroupForProfsms = (req, res) => {
Group.findAll({
include: [{
model: ContactGroup,
attributes: ['id'],
include: [{
model: Contact,
attributes: ['id', 'gsm']
}]
}],
}).then(data => {
// i am getting json here// how to do map here?
return res.status(200).send(data);
}).catch(err => {
return res.status(400).send(err.message);
});
};