在javascript中使用map功能

时间:2017-12-21 10:12:04

标签: javascript arrays json object

我的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中使用 map 方法?

我想要最终输出

[
    {
        "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);
    });
};

5 个答案:

答案 0 :(得分:2)

您可以映射数组,并使用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);




答案 1 :(得分:2)

使用map(或forEach

data.map( s => ( s.contactsCount = s.contactgroups.length, s ) );

<强>演示

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

// ...    
.then((data) => 
    res.status(200).send(data.map((i) => 
        ((i.contactsCount = i.contactgroups.length), i))))
// ...

...但不清楚为什么要这样做,因为只需检索contactgroups数组的长度就是微不足道的。

答案 3 :(得分:0)

您可以使用如下所示的data.map(contactgroupcount)。

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

答案 4 :(得分:0)

最后这个对我有用:

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