Classify JS arrays (if the id is the same, a new array will be formed)

Original address: https://blog.csdn.net/TKP666/article/details/122306087#comments_23468904
(copy a copy, so as not to delete the original)
I encountered a requirement: to classify the members in an array (the members in the array have the same id, then group them into one category), so I wrote a demo example, as follows:

 let arr = [
        {
    
    
          key: "a",
          value: 1,
        },
        {
    
    
          key: "a",
          value: 2,
        },
         {
    
    
          key: "b",
          value: 3,
        },
        {
    
    
          key: "b",
          value: 4,
        },
        {
    
    
          key: "c",
          value: 5,
        },
      ];
      //先遍历取出搜索的key(因为要根据不同的key生成数组)
      let keys = arr.map((val) => {
    
    
        return val.key;
      });
      //数组去重,这样就得到了一共有多少个key
      keys = [...new Set(keys)];
      //声明一个target来装数组
      let target = {
    
    };
      //将target对象的每一项设成数组格式
      keys.forEach((val) => {
    
    
        target[val] = [];
      });
      //如果key值相等,则push到同一个数组
      arr.map((val) => {
    
    
        keys.some((value) => {
    
    
          if (val.key == value) {
    
    
            target[value].push(val);
            return;
          }
        });
      });
      //target是一个对象,里面是分完类的数组的合集,
      console.log(target);
      //这是对象里的第一个分类的数组
      console.log(target[Object.keys(target)[0]] );
      
//最后组成的target是这样的一个对象数组
      target={
    
    
		a:[
		{
    
    
          key: "a",
          value: 1,
        },
        {
    
    
          key: "a",
          value: 2,
        }
       ],
        b:[
		{
    
    
          key: "b",
          value: 3,
        },
        {
    
    
          key: "b",
          value: 4,
        }
       ],
       c:[
       {
    
    
          key: "c",
          value: 5,
        }
       ]
	}

Later, I found a more optimized method, which generates an array of arrays:

//处理数组的方法
function mapArray(target) {
    
    
        let obj = {
    
    };
        result = [];
        target.map((item) => {
    
    
          let key = item.key;
          //如果有这个key,就push一个对象
          if (obj[key]) {
    
    
            obj[key].push(item);
          //如果没有这个key,就把对象设置成数组格式(方便后面push进去)
          } else {
    
    
            obj[key] = [item];
          }
        });
        //根据obj中有几个对象,挨个push进去,组成最后的数组
        for (const key in obj) {
    
    
          const element = obj[key];
          result.push(element);
        }
        return result;
      }
 console.log(mapArray(arr )); //调用方法,打印的结果如下:

[
	[{
    
    
       key: "a",
       value: 1,
      },{
    
    
       key: "a",
       value: 2,
      }],
   [{
    
    
      key: "b",
      value: 3,
      },{
    
    
      key: "b",
      value: 4,
   }],[{
    
    
      key: "c",
      value: 5,
     }],
]

Guess you like

Origin blog.csdn.net/weixin_44856917/article/details/128284737