首页 > 其他分享 >js重组复杂数据

js重组复杂数据

时间:2022-11-24 11:01:39浏览次数:42  
标签:重组 12 复杂 index js cat total type class

    <script>
      var list = [
        { type: 1, cat: "F1", class: "white", total: 12 },
        { type: 1, cat: "F2#1", class: "white", total: 12 },
        { type: 1, cat: "F2#2", class: "white", total: 12 },
        { type: 1, cat: "F2#3", class: "white", total: 12 },
        { type: 1, cat: "F3", class: "white", total: 12 },
        { type: 1, cat: "F4", class: "white", total: 12 },
        { type: 1, cat: "F1", class: "black", total: 12 },
        { type: 1, cat: "F2#1", class: "black", total: 12 },
        { type: 1, cat: "F2#2", class: "black", total: 12 },
        { type: 1, cat: "F2#3", class: "black", total: 12 },
        { type: 1, cat: "F3", class: "black", total: 12 },
        { type: 1, cat: "F4", class: "black", total: 12 },
        { type: 2, cat: "F1", class: "white", total: 12 },
        { type: 2, cat: "F2#1", class: "white", total: 12 },
        { type: 2, cat: "F2#2", class: "white", total: 12 },
        { type: 2, cat: "F2#3", class: "white", total: 12 },
        { type: 2, cat: "F3", class: "white", total: 12 },
        { type: 2, cat: "F4", class: "white", total: 12 },
        { type: 2, cat: "F1", class: "black", total: 12 },
        { type: 2, cat: "F2#1", class: "black", total: 12 },
        { type: 2, cat: "F2#2", class: "black", total: 12 },
        { type: 2, cat: "F2#3", class: "black", total: 12 },
        { type: 2, cat: "F3", class: "black", total: 12 },
        { type: 2, cat: "F4", class: "black", total: 12 },
      ];

      function search_param(arr, key, val) {
        for (let index in arr) {
          if (key in arr[index] && arr[index][key] == val) {
            return index;
          }
        }
        return -1;
      }

      const res = [];
      for (let i in list) {
        const index_type = search_param(res, "type", list[i]["type"]);
        if (index_type == -1) {
          res.push({
            type: list[i]["type"],
            cat: [
              {
                sub_cat: list[i]["cat"],
                total: list[i]["total"],
                data: [
                  {
                    class: list[i]["class"],
                  },
                ],
              },
            ],
          });
        } else {
          const index_sub_cat = search_param(res[index_type]["cat"], "sub_cat", list[i]["cat"]);
          if (index_sub_cat == -1) {
            res[index_type]["cat"].push({
              sub_cat: list[i]["cat"],
              total: list[i]["total"],
              data: [
                {
                  class: list[i]["class"],
                },
              ],
            });
          } else {
            res[index_type]["cat"][index_sub_cat]["total"] += list[i]["total"];
            res[index_type]["cat"][index_sub_cat]["data"].push({
              class: list[i]["class"],
            });
          }
        }
      }

      console.log(res);
    </script>

 

标签:重组,12,复杂,index,js,cat,total,type,class
From: https://www.cnblogs.com/caroline2016/p/16921143.html

相关文章

  • NodeJS下载文件并显示下载进度
    constfs=require('fs');constrequest=require("request")constprogress=require('progress-stream')consthttps=require("https")consthttp=require("h......
  • js 横向自动滚动
    <divref="scrollDiv"class="prime-tip"><divid="scroll_begin"class="item-wrap"><spanv-for="iteminpromotionInfoVo?.promotionConfigList":key="......
  • js提取对象数组中的某一个属性组成新数组
    例:1arrold=[23{name:"name1",age:"1"},4{name:"name2",age:"2"},5{name:"name3",age:"3"},6{name:"name4",age:"4"},......
  • JavaScript 面向对象(番外)JS字面量
    javascript字面量在JavaScript里面,字面量包括:字符串字面量(stringliteral)、数组字面量(arrayliteral)和对象字面量(objectliteral),另外还有函数字面量(function......
  • JavaScript--href调用JS方法和href="#"与href="javascript:void(0)"
    关于href属性<a>标签的href属性用于指定超链接目标的URL。超链接的URL可能的值:绝对URL-指向另一个站点(比如href="http://www.example.com/index.htm")相......
  • JavaScript 面向对象(番外)JS原始类型和引用类型
    书摘来自异步社区《JavaScript面向对象精要》一书中的第1章,第1.1节第1章原始类型和引用类型大多数开发者在使用Java或C#等基于类的语言的过程中学会了面向对象编程。由......
  • JS函数总和
    函数定义: JavaScript使用关键字function定义函数。 函数可以通过声明定义,也可以是一个表达式。 函数声明语法: parameters:参数 functionname:函数名称 ......
  • js013-js分离的DOM操作
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><tableborder="1"><thead><tr>......
  • JS验证及错误调试
    /* try语句测试代码块的错误。 catch语句处理错误。 throw语句创建自定义错误。 finally语句在try和catch语句之后,无论是否有触发异常,该语句都会执行......
  • springboot整合jsp打包
    本文主要分享了对整合jsp的springboot项目打jar包,如何正确的配置maven pom。二、打包配置1.pom.xml添加spring-boot-maven-plugin配置<build><plugins>......