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 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表