real47LEBJ 2022-05-09 16:47 采纳率: 95.7%
浏览 304
已结题

vue接收数组集合后根据条件生成新的数组

以下是获取json的方法

 methods: {
    getPostion: function () {
      axios({
        method: 'post',
        url: 'http://127.0.0.1:8090/commonInterface/getLastLocationInfo',
        headers: {
          'accName': 'zhanwu',
          'accPassword': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1xxx',
          'accImei': '123',
          'requestd': '',
          'Content-Type': 'application/x-www-form-urlencoded'
        },
      }).then(res => {
        console.log(res.data.data)
}

下图为console.log(res.data.data)打印出的内容。目的是把locationInfo这个集合中的48条数组中locationName相同的所有数组存成一个新的集合,locationName有24种

img

  • 写回答

7条回答 默认 最新

  • web修理工 2022-05-09 19:01
    关注
    
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            let arr = {
                "locationInfo":
                    [
                        {
                            "locationAltitude": "23", "locationDevicenum": "00000001",
                            "locationDevicetype": "0", "locationDirection": "138", "locationId": "1",
                            "locationLatitude": "134.1213", "locationLongitude": "118.2121", "locationMsgtype": "1",
                            "locationName": "地点1", "locationSpeed": "3", "locationStarnumber": "1",
                            "locationTime": 1646133123000
                        },
                        {
                            "locationAltitude": "23", "locationDevicenum": "00000002",
                            "locationDevicetype": "0", "locationDirection": "138", "locationId": "4",
                            "locationLatitude": "134.1213", "locationLongitude": "118.2121", "locationMsgtype": "1",
                            "locationName": "地点2", "locationSpeed": "3", "locationStarnumber": "1",
                            "locationTime": 1646119203000
                        },
                        {
                            "locationAltitude": "23", "locationDevicenum": "00000002",
                            "locationDevicetype": "0", "locationDirection": "138", "locationId": "4",
                            "locationLatitude": "134.1213", "locationLongitude": "118.2121", "locationMsgtype": "1",
                            "locationName": "地点2", "locationSpeed": "3", "locationStarnumber": "1",
                            "locationTime": 1646119203000
                        },
                        {
                            "locationAltitude": "23", "locationDevicenum": "00000002",
                            "locationDevicetype": "0", "locationDirection": "138", "locationId": "4",
                            "locationLatitude": "134.1213", "locationLongitude": "118.2121", "locationMsgtype": "1",
                            "locationName": "地点2", "locationSpeed": "3", "locationStarnumber": "1",
                            "locationTime": 1646119203000
                        }
                    ]
            }
    
            function arr_type(arrList) {
                let list = []
                arrList.forEach((item) => {
                    let index = list.findIndex((zitem) => {
                        return zitem.some((sitem, sindex) => { return sitem.locationName == item.locationName })
                    });
                    if (~index) {
                        list[index].push(item)
                    } else {
                        list[list.length] = [item]
                    }
                })
                return list
            }
            console.log(arr_type(arr.locationInfo))
        </script>
    </body>
    
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 创建了问题 5月9日

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题