jQuery将数组转化成对象

2022-11-12 11:56:09 浏览数 (1)

要求 将图一格式数据转化成图二格式数据

图一

图二

原始json数据

代码语言:javascript复制
[{
    "id": 15,
    "paramName": "distance",
    "paramValue": "1",
    "description": "定位前后位置在一定距离内不跳动 单位:米",
    "companyId": 1,
    "paramTypeId": 2
}, {
    "id": 110,
    "paramName": "floorNum",
    "paramValue": "centerIndex",
    "description": "楼层编号",
    "companyId": 1,
    "paramTypeId": 2
}, {
    "id": 119,
    "paramName": "gltfFloor",
    "paramValue": "1",
    "description": "gltf楼层",
    "companyId": 1,
    "paramTypeId": 2
},{
    "id": 94,
    "paramName": "imageryProvider",
    "paramValue": "0",
    "description": "0:无室外图;1:矢量室外图;2:影像图",
    "companyId": 1,
    "paramTypeId": 2
} , {
    "id": 113,
    "paramName": "TwoDim",
    "paramValue": "2",
    "description": "2D地图,1矢量图 2切片图",
    "companyId": 1,
    "paramTypeId": 2
}]

转换代码

代码语言:javascript复制
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>复选框checkbox自定义样式</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
      
    </head>
    <body>
        <div class="modal-body form">
         
        </div>
    </body>
    <script type="text/javascript">
        $.ajax({
            url: "test.json",
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                
              const obj = {}
              data.forEach(a => {
              obj[a.paramName] = a
              })
              console.log(obj)//你要的对象
            }
        });
    </script>
</html>

查看结果

0 人点赞