zoukankan      html  css  js  c++  java
  • js将数组对象中,以某个值相同的对象合并成一个;即把某个值相同的对象内容合并成一个

    以下列数据为例,总共有13条数据, 把label相同的数据,里面的children合并成一个             代码在最后

     1、初始化数据

    2.结果

    3.代码

          let testArr = [
            {
              label: "考区/考点",
              prop: "deptNames",
              row: true,
              search: true,
               200,
              editDisabled: true,
              expand: true,
              hide: true,
              defaultExpandAll: false,
              type: "tree",
              dicUrl: "/test/boltdept/getAreaAndPointTree",
              props: {
                label: "deptName",
                value: "deptId",
                children: "children",
              },
            },
            {
              label: "机构",
              prop: "deptName",
              type: "select",
              row: true,
              editDisplay: false,
              search: false,
              dicUrl: "/test/dict/getDictsByCode?dictCode=kw_tag_type",
              props: {
                label: "dictValue",
                value: "dictKey",
              },
            },
            {
              label: "单位等级",
              prop: "deptLeve",
              editDisplay: false,
              search: true,
              hide: true,
              row: true,
              span: 15,
              type: "select",
              dicQuery: {
                materialDictType: 0,
              },
              dicUrl: "/test/materialdictsetting/selectViewLevelList",
              props: {
                label: "dictValue",
                value: "dictKey",
              },
            },
            {
              label: "机构等级",
              row: true,
              search: false,
              editDisabled: true,
               200,
              type: "select",
              prop: "deptLevel",
              dicUrl: "/test/dict/getDictsByCode?dictCode=kw_dept_level",
              props: {
                label: "dictValue",
                value: "dictKey",
              },
            },
            {
              label: "语文",
              prop: "203311500001",
              row: true,
              children: [
                {
                  label: "备用卷",
                  prop: "203311500001_85975601163943937",
                },
              ],
            },
            {
              label: "数学",
              prop: "203311500002",
              row: true,
              children: [
                {
                  label: "备用卷",
                  prop: "203311500002_85975601163943937",
                },
              ],
            },
            {
              label: "数学",
              prop: "203311500002",
              row: true,
              children: [
                {
                  label: "test_3",
                  prop: "203311500002_84857933463703554",
                },
              ],
            },
            {
              label: "英语",
              prop: "203311500004",
              row: true,
              children: [
                {
                  label: "备用卷",
                  prop: "203311500004_85975601163943937",
                },
              ],
            },
            {
              label: "英语",
              prop: "203311500004",
              row: true,
              children: [
                {
                  label: "test_3",
                  prop: "203311500004_84857933463703554",
                },
              ],
            },
            {
              label: "印刷材料",
              prop: null,
              row: true,
              children: [
                {
                  label: "cs",
                  prop: "84563743102074881",
                },
              ],
            },
            {
              label: "印刷材料",
              prop: null,
              row: true,
              children: [
                {
                  label: "test_1",
                  prop: "84507262931640321",
                },
              ],
            },
            {
              label: "印刷材料",
              prop: null,
              row: true,
              children: [
                {
                  label: "测试啊",
                  prop: "84172603177795585",
                },
              ],
            },
            {
              label: "印刷材料",
              prop: null,
              row: true,
              children: [
                {
                  label: "test",
                  prop: "84173239118168066",
                },
              ],
            },
          ];
          console.log(testArr);
    
          function mergeArr(testArr) {
            var tempArr = [];
            var afterData = [];
            for (let i = 0; i < testArr.length; i++) {
              if (tempArr.indexOf(testArr[i].label) === -1) {
                afterData.push({
                  label: testArr[i].label,
                  children: [testArr[i]],
                  prop: testArr[i].prop,
                });
                tempArr.push(testArr[i].label);
              } else {
                for (let j = 0; j < afterData.length; j++) {
                  if (
                    (afterData[j].label == testArr[i].label) &
                    testArr[i].hasOwnProperty("children")
                  ) {
                    afterData[j].children.push(testArr[i]);
                    break;
                  }
                }
              }
            }
            console.log(afterData, "初次结果");
    
            afterData.forEach((item) => {
              item.childrenOne = [];
              item.children.forEach((y) => {
                //   console.log(y, "----", y.children);
                if (y.children != undefined) {
                  item.childrenOne.push(y.children[0]);
                }
              });
            });
    
            let jj = afterData;
            jj.forEach((item, index) => {
              item.children = [];
              item.children = item.childrenOne;
              delete jj[index].childrenOne;
              if (jj[index].children.length == 0) {
                delete jj[index].children;
              }
            });
    
            console.log(jj);
            return jj
          }
    
          console.log(this.mergeArr(testArr), "---最终结果");
  • 相关阅读:
    Mac上的USB存储设备使用痕迹在新版操作系统有所变化
    Beware of the encrypted VM
    A barrier for Mobile Forensics
    Second Space could let suspect play two different roles easily
    Take advantage of Checkra1n to Jailbreak iDevice for App analysis
    Find out "Who" and "Where"
    Where is the clone one and how to extract it?
    Downgrade extraction on phones running Android 7/8/9
    高版本安卓手机的取证未来
    How to extract WeChat chat messages from a smartphone running Android 7.x or above
  • 原文地址:https://www.cnblogs.com/m1754171640/p/14654585.html
Copyright © 2011-2022 走看看