美文网首页
js 数组格式转换

js 数组格式转换

作者: 小纸人儿 | 来源:发表于2021-07-15 15:45 被阅读0次

    实际需求中总是遇到,后端给的一维数组需要转成一定的格式才能在业务中去使用或操作,下面是我常遇到的几种转化:

    1、一维数组转json

    初始数组:
    const arr = [
                {
                  classKey: "jblx",
                  classKeyName: "基本类型",
                  key: "contract_name",
                  name: "合同名称"
                },
                {
                  classKey: "jblx",
                  classKeyName: "基本类型",
                  key: "contract_name1",
                  name: "合同名称1",
                },
                {
                  classKey: "qsxx",
                  classKeyName: "签署信息",
                  key: "cust_credit",
                  name: "用电户社会信用代码"
                },
                {
                  classKey: "qsxx",
                  classKeyName: "签署信息",
                  key: "cust_credit1",
                  name: "用电户社会信用代码1"
                },
     ]
      转化成
      {
        jblx: [
            {
              classKey: "jblx"
              classKeyName: "基本类型"
              key: "contract_name"
              name: "合同名称"
            },
           {
             classKey: "jblx"
             classKeyName: "基本类型"
             key: "contract_name1"
             name: "合同名称1"
           }
          ],
        qsxx: [
            {
              classKey: "qsxx"
              classKeyName: "签署信息"
              key: "cust_credit"
              name: "用电户社会信用代码"
            },
           {
             classKey: "qsxx"
             classKeyName: "签署信息"
             key: "cust_credit1"
             name: "用电户社会信用代码1"
           }
          ]
      }
    
    arr.forEach((item) => {
        if (obj[item.classKey] == undefined) {
          obj[item.classKey] = []
       }
       obj[item.classKey].push(item)
    })
    
    里面数组也想换成json的话:
    arr.forEach((item) => {
        if (obj[item.classKey] == undefined) {
          obj[item.classKey] = {}
       }
       obj[item.classKey][item.key] = item
    })
      
    

    2、一维数组拆分成多维数组

    上面初始数组转化成:
    [
      {
        classKey: "jblx",
        classKeyName: "基本类型",
        data: [
            {
              key: "contract_name",
              name: "合同名称"
            },
           {
             key: "contract_name1",
             name: "合同名称1"
           }
          ]
      },
      {
        classKey: "qsxx",
        classKeyName: "签署信息",
        data: [
            {
              key: "cust_credit"
              name: "用电户社会信用代码"
            },
           {
             key: "cust_credit1"
             name: "用电户社会信用代码1"
           }
          ]
        }
    ]
    
    let keys = [];
    let newArr = [];
      data.map((v)=>{
        const data = {
            name:v.name,
            key:v.key
        }
        let index = keys.indexOf(v.classKey)
        if (index>-1) {
            newArr[index].data.push(data)
        } else {
            keys.push(v.classKey)
            newArr.push({
                classKey: v.classKey,
                classKeyName: v.classKeyName,
                data: [data],
            })
        }
      })
    
    console.log(newArr);
    

    相关文章

      网友评论

          本文标题:js 数组格式转换

          本文链接:https://www.haomeiwen.com/subject/wytmpltx.html