js转换数据格式

68 阅读3分钟

原数组

let arr =[
    [
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-z22dj"
            },
            "value": [
                1693794269,
                "716800"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-fld5m"
            },
            "value": [
                1693794269,
                "33693696"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-tdp49"
            },
            "value": [
                1693794269,
                "720896"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-ncj9k"
            },
            "value": [
                1693794269,
                "10756096"
            ]
        }
    ],
    [
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-ncj9k"
            },
            "value": [
                1693794269,
                "1073741824"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-tdp49"
            },
            "value": [
                1693794269,
                "1073741824"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-z22dj"
            },
            "value": [
                1693794269,
                "1073741824"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-fld5m"
            },
            "value": [
                1693794269,
                "1073741824"
            ]
        }
    ],
    [],
    [
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-tdp49"
            },
            "value": [
                1693794269,
                "10737418240"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-z22dj"
            },
            "value": [
                1693794269,
                "10737418240"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-fld5m"
            },
            "value": [
                1693794269,
                "10737418240"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-ncj9k"
            },
            "value": [
                1693794269,
                "10737418240"
            ]
        }
    ],
    [],
    [
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-z22dj"
            },
            "value": [
                1693794270,
                "73728"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-fld5m"
            },
            "value": [
                1693794270,
                "19247104"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-tdp49"
            },
            "value": [
                1693794270,
                "69632"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-ncj9k"
            },
            "value": [
                1693794270,
                "5767168"
            ]
        }
    ],
    [
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-z22dj"
            },
            "value": [
                1693794270,
                "0"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-fld5m"
            },
            "value": [
                1693794270,
                "0"
            ]
        },
        {
            "metric": {
                "pod": "test-springdemo-b9b59bc98-tdp49"
            },
            "value": [
                1693794270,
                "0"
            ]
        },
        {
            "metric": {
                "pod": "nginx-test-555bd449bf-ncj9k"
            },
            "value": [
                1693794270,
                "0"
            ]
        }
    ]
]

要改成的数组格式:(ABCDEFG,这个key值可以随便改),改成elemeentui中的table中的格式,arr1中有7组数据,代表table中有7列

微信图片_20230904150004.png

let arr2 =[
{
        name: 'test-springdemo-b9b59bc98-z22dj',
        A: '716800', // 内存使用量
        B: '1073741824', // 内存 Requests
        C: 0', // 内存使用率(/Requests)
        D: '10737418240', // 内存 Limits
        E: '0', // 内存使用率(/Limits)
        F: ' 73728', // 内存使用量(RSS)
        G: '0 bytes' // 内存使用量(Cache)
      }
]

方法1:

    function getResult(arr) {
      let map = new Map();
      let a = []
      arr.forEach(function (it) {
        if(it.length===0){
            let keys = Object.keys(a[0])
            a.forEach(it=>{
              it['value'+keys.length] = 0
            })
        }
        it.forEach(function (item){
          if (!map.get(item.metric.pod) && map.get(item.metric.pod)!==0) {
            map.set(item.metric.pod, a.length)
            a[a.length] = {
              name: item.metric.pod,
              value1: item.value[1]
            }
          } else {
            let keys = Object.keys(a[map.get(item.metric.pod)])
            a[map.get(item.metric.pod)]['value'+keys.length] = item.value[1]
          }
        })
      })
      console.log('aaaa', a)
      return a
    },

方法2:

let word = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    function changeAFun(arr,activeIndex){
        let num = 0
        let obj = {}
        if(activeIndex>=arr[0].length){
                return
        }
        //外循环
        arr.forEach((item,index)=>{
                //判断当前数组是否为空数组
                if(!item.length){
                        obj[word[num]]=0
                        num+=1
                        return
                }
                //内部数组循环
                item.forEach((sItem,sIndex)=>{
                        //
                        if(sItem.metric.pod===arr[0][activeIndex].metric.pod){
                                obj.name = sItem.metric.pod
                                obj[word[num]] = sItem.value[1]
                                num+=1
                        }
                })
        })
        arrFlattening.push(obj)
        changeAFun(arr,activeIndex+1)
},

第三种方法:

//第一个参数原数组 第二个参数按顺序自定义各列的字段
const fields = ['Pod', '内存使用量WSS', '内存Requests', '内存使用率Requests', '内存Limits', '内存使用率Limits', '内存使用率RSS', '内存使用量Cache']
    compare(str1 = '', str2 = '') {
      let str = str1.length > str2.length ? str1 : str2
      for(let i = 0; i < str.length; i++) {
        return str1.charCodeAt - str2.charCodeAt
      }
    },
    mapTableData(arr, fields) {
      let len = 0
      let fieldFront = fields[0]
      let fieldEnd = fields.slice(1)
      arr.forEach(item => {
        item.length > len && (len = item.length)
        item.length && item.sort((a, b) => this.compare(a.metric.pod, b.metric.pod))
      })
      return Array.from({length: len}).reduce((pre, cur, i) => {
        let row = {}
        arr.forEach((it, idx) => {
          if(it.length) {
            row[fieldFront] = it[i].metric.pod
            row[fieldEnd[idx]] = it[i] ? it[i].value[1] : null
          } else {
            row[fieldEnd[idx]] = 0
          }
        })
        pre.push(row)
        return pre
      }, [])
    },