我的json反应结构:
[
{
"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": []
}
]在contactgroups数组中有两个对象,所以我需要将contactsCount作为2返回,
如果我在contactgroups中有一个对象,我需要将contactsCount作为1返回,是否可以使用Javascript中的映射方法?
我想要最后的输出
[
{
"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": []
}
]现在我的最后输出中包含了contactsCount。
这是我的api代码:
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);
});
};发布于 2017-12-21 11:28:55
最后,这个对我有用:
exports.getNewGroupForProfsms = (req, res) => {
Group.findAll({
include: [{
model: ContactGroup,
attributes: ['id'],
include: [{
model: Contact,
attributes: ['id', 'gsm']
}]
}],
}).then(data => {
// change happen here
return res.status(200).send(data.map((x) => {
// assign contactsCount to each row
return Object.assign({
contactsCount: x.contactgroups.length,
}, x.toJSON()) // update toJSON have a try
}));
}).catch(err => {
return res.status(400).send(err.message);
});
};发布于 2017-12-21 10:15:28
使用map (或forEach)
data.map( s => ( s.contactsCount = s.contactgroups.length, s ) );Demo
var data = [
{
"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"
}
}
]
}
];
data.map( s => ( s.contactsCount = s.contactgroups.length, s ) );
console.log(data);
发布于 2017-12-21 10:14:54
您可以映射数组,并使用Object#assign返回一个带有计数的新对象:
const arr = [{"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"}}]}];
const result = arr.map((o) => Object.assign({ contactsCount: o.contactgroups.length || 0 }, o));
console.log(result);
https://stackoverflow.com/questions/47922583
复制相似问题