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

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日

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器