【问题标题】:Using map function in javascript在javascript中使用地图功能
【发布时间】:2017-12-21 10:12:04
【问题描述】:

我的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 中的ma​​p 方法?

我想要像这样的最终输出

[
    {
        "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": []
    }
]

现在看到我的最终输出中有contactCount。

这是我的 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);
    });
};

【问题讨论】:

    标签: javascript arrays json object


    【解决方案1】:

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

    【讨论】:

    • 谢谢,看到我更新的问题,我想得到这个回应。
    【解决方案2】:

    使用map(或forEach

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

    演示

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

    【讨论】:

    • 谢谢,看到我更新的问题,我想得到这个回应。
    • 你试过在那个地方使用这个代码吗?您只需将 data 替换为适当的变量或 JSON 路径即可。
    • 是的,我没有获得联系人计数。你能用我的api代码发布完整的答案吗?这对我真的很有帮助
    • 你能分享一下你的api代码中data的结构吗?
    • 这一行放在return语句之前data = data.map( s => ( s.contactsCount = s.contactgroups.length, s ) );
    【解决方案3】:
    // ...    
    .then((data) => 
        res.status(200).send(data.map((i) => 
            ((i.contactsCount = i.contactgroups.length), i))))
    // ...
    

    ...但尚不清楚您为什么要这样做,因为简单地检索 contactgroups 数组的长度是微不足道的。

    【讨论】:

    • 这是我的确切问题,stackoverflow.com/questions/47919818/…
    • 我无法得到答案,所以我这样做了
    • 我添加了一个缺少的括号,请重试。
    • 如果我通过邮递员发送请求,我仍然没有收到contactCount 响应,我认为发布完整的api 代码对我真的很有帮助:)
    • 检查您是否访问了正确的端点并且服务器已重新启动。插入调试器以检查代码是否正在运行。
    【解决方案4】:

    您可以像下面这样使用 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);

    【讨论】:

      【解决方案5】:

      最后这个对我有用:

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

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 1970-01-01
        • 2019-01-31
        • 1970-01-01
        • 1970-01-01
        • 2019-09-13
        • 2020-10-31
        • 2020-04-02
        • 2018-11-05
        相关资源
        最近更新 更多