JS——把数组中拥有相同值的部分放在一起

tableData2: [
        {
          name: "项目1",
          birth: "80m²",
          status: "已确认",
          time: "2020-01-01",
          zhuanye: "金水区资源局",
          isOption: false,
        },
        {
          name: "项目2",
          birth: "80m²",
          status: "已确认",
          time: "2020-01-01",
          zhuanye: "金水区资源局",
          isOption: false,
        },
        {
          name: "项目3",
          birth: "80m²",
          status: "待确认",
          time: "--  --",
          zhuanye: "金水区税务局",
          isOption: true,
        },
        {
          name: "项目4",
          birth: "80m²",
          status: "已确认",
          time: "--  --",
          zhuanye: "金水区资源局",
          isOption: true,
        },
      ],
      

js部分

     checkSameData(tableData2){
          let cache = {};  //存储的是键是zhuanye 的值,值是zhuanye 在indeces中数组的下标
          let indices = [];  //数组中每一个值是一个数组,数组中的每一个元素是原数组中相同zhuanye的下标
          tableData2.map((item,index)=>{
            let zhuanye = item.zhuanye;
            let _index = cache[zhuanye];
            if(_index!==undefined){
                 indices[_index].push(index)
            }else{
                cache[zhuanye] = indices.length
                indices.push([index])
            }
          })
          let result = [];
          indices.map((item)=>{
            item.map((index)=>{
                result.push(tableData2[index])
            })
          })
          this.tableData2 = result
      },
 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容