0

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

5 Answers 5

2

Use map (or 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);

Sign up to request clarification or add additional context in comments.

9 Comments

thank you, see my updated question i want to get this in response.
Did you tried using this code at that place? You just need to replace data with appropriate variable or JSON path.
yes i dont get contactsCount. can you post full answer with my api code? it will be really helpful to me
can you share the structure of the data in your api code?
Just put this line before return statement data = data.map( s => ( s.contactsCount = s.contactgroups.length, s ) );
|
1

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

1 Comment

thank you, see my updated question i want to get this in response.
1
// ...    
.then((data) => 
    res.status(200).send(data.map((i) => 
        ((i.contactsCount = i.contactgroups.length), i))))
// ...

...but it is not clear why you want to do this, given that simply retrieving the length of the contactgroups array is trivial.

6 Comments

This is my exact problem , stackoverflow.com/questions/47919818/…
i cant get answer , so i am doing like this
I added a missing parenthesis, pls try again.
if i send request via postman still i dont get contactsCount in response, i think posting full api code will be really helpful to me :)
Check you are hitting the right endpoint and that the server has been restarted. Stick a debugger in to check the code is being run.
|
0

You can use data.map(contactgroupcount) like below.

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"
                }
            }
        ]
    }
]

    function contactgroupcount(obj) {
       obj["contactsCount"] = obj.contactgroups.length;
        return obj;
    }

    data.map(contactgroupcount);

Comments

0

Finally this one worked for me:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.