数组格式转换(二维数组转数组对象)

数组格式转换

let data = {
        "合同容量": [
          "小",
          "中等",
          "较大",
          "大"
        ],
        "月度敏感": [
          "敏感",
          "不敏感"
        ],
        "负荷敏感性": [
          "稳定",
          "不稳定"
        ]
    }
let selectData = []
let values = []
Object.values(data).map((val, i) => {
        let arr1 = []
        val.map((item,ind) => {
          arr1.push({
            name: item,
            value: item
          })
        })
        values.push(arr1)
})

Object.keys(data).map((key,index) => {
        selectData.push({
            value: key,
            name: key,
            children: values[index]
          })
})

结果

selectData = [
    {
        "name": "合同容量", 
        "value": "1", 
        "children": [
            {
                "name": "小", 
                "value": "1"
            }, 
            {
                "name": "中等", 
                "value": "2"
            }, 
            {
                "name": "较大", 
                "value": "3"
            }, 
            {
                "name": "大", 
                "value": "4"
            }
        ]
    }, 
    {
        "name": "月度敏感", 
        "value": "2", 
        "children": [
            {
                "name": "敏感", 
                "value": "1"
            }, 
            {
                "name": "不敏感", 
                "value": "2"
            }
        ]
    }, 
    {
        "name": "负荷敏感性", 
        "value": "3", 
        "children": [
            {
                "name": "稳重", 
                "value": "1"
            }, 
            {
                "name": "不稳重", 
                "value": "2"
            }
        ]
    }
]

你可能感兴趣的:(数组格式转换(二维数组转数组对象))