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

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日

悬赏问题

  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 zabbix6.4与frp如何进行联动