js converts one-dimensional array to two-dimensional array and groups

let arr = [{
    
    a:'W',b:'W01'},{
    
    a:'W',b:'W02'},{
    
    a:'WC',b:'WC01'},{
    
    a:'WC',b:'WC02'},{
    
    a:'WC',b:'WC02'},{
    
    a:'WC',b:'WC02'}]
let map= {
    
    };
arr.forEach(item=>{
    
    
       if(map[item.a]){
    
    
           map[item.a].push(item.b);
       }else{
    
    
           map[item.a] = [item.b];
       } 
   })
   console.log(map);

The output effect is as shown in the figure
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44812604/article/details/128824991