我有一个具有不同审批者级别的数组。我需要组合相似级别的对象,并将它们与Name + counter组合在一起。

"APPROVERS": [
        {
            "LEVEL": "L5",
            "EMAIL": "[email protected]",
            "FULLNAME": "FNAME",
            "POSITION": "FPOS",
            "SLA": "48",
            "STATUS": "INITIAL"
        },
        {
            "LEVEL": "L4",
            "EMAIL": "[email protected]",
            "FULLNAME": "JNAME",
            "POSITION": "JPOS",
            "SLA": "48",
            "STATUS": "INITIAL"
        },
        {
            "LEVEL": "L5",
            "EMAIL": "[email protected]",
            "FULLNAME": "LNAME",
            "POSITION": "GPOS",
            "SLA": "48",
            "STATUS": "INITIAL"
        },
        {
            "LEVEL": "L5",
            "EMAIL": "[email protected]",
            "FULLNAME": "TNAME",
            "POSITION": "CPOS",
            "SLA": "48",
            "STATUS": "INITIAL"
        }
    ]

我需要将同一级别的对象合并为一个,并提供唯一的名称。
 "APPROVERS": [
        {
            "LEVEL": "L5",
            "EMAIL1": "[email protected]",
            "FULLNAME1": "FNAME",
            "POSITION1": "FPOS",
            "SLA1": "48",
            "STATUS1": "INITIAL",
            "EMAIL2": "[email protected]",
            "FULLNAME2": "LNAME",
            "POSITION2": "GPOS",
            "SLA2": "48",
            "STATUS2": "INITIAL",
            "EMAIL3": "[email protected]",
            "FULLNAME3": "TNAME",
            "POSITION3": "CPOS",
            "SLA3": "48",
            "STATUS3": "INITIAL"
        },
        {
            "LEVEL": "L4",
            "EMAIL": "[email protected]",
            "FULLNAME": "JNAME",
            "POSITION": "JPOS",
            "SLA": "48",
            "STATUS": "INITIAL"
        }
    ]

我仅尝试通过循环数组来组合EMAIL,但无法实现结果。建议。
var result = [];
    var i=0
    APPROVERS.forEach(function(obj) {
      var id = obj.LEVEL
      if(!this[id]) result.push(this[id] = obj);
      else this[id].EMAIL += obj.EMAIL+i;
      i++;
    }, Object.create(null));

    console.log(result)

最佳答案

通过放置标记lodash,我猜您不介意使用它。我不确定您是否理解它,但是我已尽力在简洁性和可读性之间实现的平衡。

function groupByLevel(approvers) {

  const group = _.groupBy(approvers, 'LEVEL');
  // console.log(group); // try logging to see what we have

  return Object.entries(group).map( ([LEVEL, array]) => {

    return array.reduce((acc, cur, idx) => ({
      ...acc,
      ['EMAIL'    + (idx + 1)]: cur.EMAIL    ,
      ['FULLNAME' + (idx + 1)]: cur.FULLNAME ,
      ['POSITION' + (idx + 1)]: cur.POSITION ,
      ['SLA'      + (idx + 1)]: cur.SLA      ,
      ['STATUS'   + (idx + 1)]: cur.STATUS   ,

    }), { LEVEL });
  })
}












var APPROVERS = [
  {
    LEVEL: 'L5',
    EMAIL: '[email protected]',
    FULLNAME: 'FNAME',
    POSITION: 'FPOS',
    SLA: '48',
    STATUS: 'INITIAL'
  },
  {
    LEVEL: 'L4',
    EMAIL: '[email protected]',
    FULLNAME: 'JNAME',
    POSITION: 'JPOS',
    SLA: '48',
    STATUS: 'INITIAL'
  },
  {
    LEVEL: 'L5',
    EMAIL: '[email protected]',
    FULLNAME: 'LNAME',
    POSITION: 'GPOS',
    SLA: '48',
    STATUS: 'INITIAL'
  },
  {
    LEVEL: 'L5',
    EMAIL: '[email protected]',
    FULLNAME: 'TNAME',
    POSITION: 'CPOS',
    SLA: '48',
    STATUS: 'INITIAL'
  }
]
console.log(groupByLevel(APPROVERS))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

10-06 11:55