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

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 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题