数据分析,数组转化,对象转化,代码总结统计

1.两批数据,需要对应其统计数据

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let a = {
      a:1,
      b:1,
      c:1,
      d:2,
      e:2,
      f:2,
      g:2
    }
    let aObj = {
      1: ['a','b','c'],
      2: ['d','e','f','g']
    }
    let b = ['a','b','c','d','e','f','b','c','f','e','a','b','c','d','e','g','b','c','d','e','a','b','c','d','e']
    let countObj = {}
    let totalObj = {}
    b.forEach(_ => {
      // 统计countObj
      if(countObj[_]){
        countObj[_] += 1
      }else{
        countObj[_] = 1
      }
      // 统计countObj
      Object.keys(aObj).forEach(a=>{
        if(totalObj[a] === undefined){
          totalObj[a]=0
        }
        if(aObj[a].indexOf(_) !== -1) {
          totalObj[a] +=1
        }
      })
    })
    console.log(countObj)
    console.log(totalObj)
  </script>
</body>
</html>

1.两批数据,需要对应其统计数据

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let a = {
      a:1,
      b:1,
      c:1,
      d:2,
      e:2,
      f:2,
      g:2
    }
    let aObj = {
      1: ['a','b','c'],
      2: ['d','e','f','g']
    }
    let b = ['a','b','c','d','e','f','b','c','f','e','a','b','c','d','e','g','b','c','d','e','a','b','c','d','e']
    let countObj = {}
    let totalObj = {}
    b.forEach(_ => {
      // 统计countObj
      if(countObj[_]){
        countObj[_] += 1
      }else{
        countObj[_] = 1
      }
      // 统计countObj
      Object.keys(aObj).forEach(a=>{
        if(totalObj[a] === undefined){
          totalObj[a]=0
        }
        if(aObj[a].indexOf(_) !== -1) {
          totalObj[a] +=1
        }
      })
    })
    console.log(countObj)
    console.log(totalObj)
  </script>
</body>
</html>

2.复制修改部分原数组最后几个元素,并添加到原数组


    var newArr = [{ "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef0", "domId": "myDom0" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef1", "domId": "myDom1" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef2", "domId": "myDom2" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef3", "domId": "myDom3" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef4", "domId": "myDom4" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef5", "domId": "myDom5" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef6", "domId": "myDom6" }, { "xxxvvv": 100, "aaaAAA": 100, "colorType": "blue", "domRef": "myRef777", "domId": "myDom777" }]
    console.log('newArr', newArr)
    const num = 5
    const len = newArr.length
    for (var i = 0; i < num; i++) {
      let index = len - (num - i)
      let obj = {}
      console.log('i', i, 'index', index, 'len', len)
      Object.keys(newArr[index]).forEach(k => {
        if (['domRef', 'domId'].indexOf(k) === -1) { obj[k] = newArr[index][k] } else { obj[k] = 'add' + newArr[index][k] }

      })
      newArr.push(obj)
    }

    console.log('newArr', newArr)



    // newArr  元数据
    //   (8)[{ … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }]
    // 0: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef0", domId: "myDom0" }
    // 1: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef1", domId: "myDom1" }
    // 2: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef2", domId: "myDom2" }
    // 3: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef3", domId: "myDom3" }
    // 4: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef4", domId: "myDom4" }
    // 5: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef5", domId: "myDom5" }
    // 6: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef6", domId: "myDom6" }
    // 7: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef777", domId: "myDom777" }
   



    // arr.html: 46 i 0 index 3 len 8
    // arr.html: 46 i 1 index 4 len 8
    // arr.html: 46 i 2 index 5 len 8
    // arr.html: 46 i 3 index 6 len 8
    // arr.html: 46 i 4 index 7 len 8
    // arr.html: 54 
    
    
    // newArr 修改后的数据
    //   (13)[{ … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }, { … }]
    // 0: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef0", domId: "myDom0" }
    // 1: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef1", domId: "myDom1" }
    // 2: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef2", domId: "myDom2" }
    // 3: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef3", domId: "myDom3" }
    // 4: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef4", domId: "myDom4" }
    // 5: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef5", domId: "myDom5" }
    // 6: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef6", domId: "myDom6" }
    // 7: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "myRef777", domId: "myDom777" }
    // 8: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "addmyRef3", domId: "addmyDom3" }
    // 9: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "addmyRef4", domId: "addmyDom4" }
    // 10: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "addmyRef5", domId: "addmyDom5" }
    // 11: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "addmyRef6", domId: "addmyDom6" }
    // 12: { xxxvvv: 100, aaaAAA: 100, colorType: "blue", domRef: "addmyRef777", domId: "addmyDom777" }
    // length: 13
    // __proto__: Array(0)
上一篇:SQL Server 帐号权限管理及编程应用(图解)


下一篇:telegraf+prometheus+grafana+aletmanager+dingtalk实现主机监控告警