【问题标题】:How to group objects according to a property如何根据属性对对象进行分组
【发布时间】:2020-10-02 12:40:42
【问题描述】:

希望有人可以帮助我,

情况: 我正在开发一个单元报告,我在其中选择所有带有 unit_id 的客户(unit_id 是与单元的关系,它们可以在哪里找到),这个选择是一个对象数组的数组,我遇到了麻烦如何根据一个特定属性对对象进行分组,我有一个数组,例如:

const clients = [
  [
    {
      ID: 1,
      NAME: 'John Doe',
      UNITS: ['02'],
    }
  ],
  [
    {
      ID: 2,
      NAME: 'Jane Doe',
      UNITS: ['01', '02'],
    }
  ],
  [
    {
      ID: 3,
      NAME: 'Doe John',
      UNITS: ['50', '15'],
    }
  ],
  [
    {
      ID: 4,
      NAME: 'Doe Jane',
      UNITS: ['30'],
    }
  ],
  [
    {
      ID: 5,
      NAME: 'Doe Jane',
      UNITS: ['15'],
    }
  ],
]

问题: 我需要用相同的“UNIT”对每个人进行分组,但我不能在一个组中拥有多个具有相同“UNIT ID”的组,我'我期待这样的结果:

const unitGroups = [
  [
    {
      UNIT_IDS: ['01', '02'],
      CLIENTS: [
        {
          ID: 1,
          NAME: 'John Doe',
        }, 
        {
          ID: 2,
          NAME: 'Jane Doe',
        }
      ]
    }
  ],
  [
    {
      UNIT_IDS: ['50', '15'],
      CLIENTS: [
        {
          ID: 3,
          NAME: 'Doe John',
        },
        {
          ID: 5,
          NAME: 'Doe Jane',
        }
      ]
    }
  ],
  [
    {
      UNIT_IDS: ['30'],
      CLIENTS: [
        {
          ID: 4,
          NAME: 'Doe Jane',
        }
      ]
    }
  ]
]

【问题讨论】:

    标签: javascript node.js logic lodash backend


    【解决方案1】:

    您可以使用reduce 来实现结果。这是一个实现:

    const clients = [ [ { ID: 1, NAME: 'John Doe', UNITS: ['02'], } ], [ { ID: 2, NAME: 'Jane Doe', UNITS: ['01', '02'], } ], [ { ID: 3, NAME: 'Doe John', UNITS: ['50', '15'], } ], [ { ID: 4, NAME: 'Doe Jane', UNITS: ['30'], } ], [ { ID: 5, NAME: 'Doe Jane', UNITS: ['15'], } ]];
    
    const result = clients.reduce((a,e)=>{
       e.forEach(({UNITS, ...rest})=>{
        const getIndex = a.findIndex(p=>p.UNIT_IDS.some(b=>UNITS.some(c=>c===b)));
        if(getIndex===-1){
           const newData = {UNIT_IDS:UNITS, CLIENTS:[].concat(rest)};
           a.push(newData);
           } else {
           a[getIndex].UNIT_IDS = [...new Set([...a[getIndex].UNIT_IDS, ...UNITS])];
           a[getIndex].CLIENTS = [...a[getIndex].CLIENTS, rest]
           }
        })
      return a;
    },[]);
    
    console.log(result);

    【讨论】:

      【解决方案2】:
      const intersection = (array1, array2) =>
        array1.filter((value) => array2.includes(value));
      
      const results = [];
      
      for (const { ID, NAME, UNITS } of clients.flat()) {
        const contain = results.find(({ UNIT_IDS }) =>
          intersection(UNIT_IDS, UNITS).length
        );
      
        if (contain) {
          contain.CLIENTS.push({ ID, NAME });
      
          if (UNITS.length > contain.UNIT_IDS.length) {
            contain.UNIT_IDS = UNITS;
          }
      
          continue;
        }
      
        results.push({
          UNIT_IDS: UNITS,
          CLIENTS: [
            { ID, NAME },
          ],
        });
      }
      
      console.log(results.map((group) => [group]));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        相关资源
        最近更新 更多