Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
    });
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can map the array, and use Object#assign to return a new object with the count:

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...