按值比较数组中的所有对象

Compare all objects in array by value against each other

我正在尝试确定数组中哪个对象的 属性 值大于其他对象,但根据某些条件。

我得到了一组代表员工及其直线经理的对象,如果一个人向直线经理报告的情况与其他人相同,但他们的 Rank 更高,我希望能够反映在属性 LineManager 加 1。一个很好的例子是 Tomas 他的 Rank 为 6 并向 Peter 汇报,但所有其他人也向 Peter 汇报Peter Rank 为 5。

我已经设法得到正确的输出:

const data = [
  {
    Name: 'Peter',
    ReportsTo: '',
    LineManager: '',
    Rank: 4
  },
  {
    Name: 'Tom',
    ReportsTo: 'Peter',
    LineManager: 0,
    Rank: 5
  },
  {
    Name: 'Adam',
    ReportsTo: 'Peter',
    LineManager: 0,
    Rank: 5
  },
  {
    Name: 'Maria',
    ReportsTo: 'Peter',
    LineManager: 0,
    Rank: 5
  },
  {
    Name: 'Fiona',
    ReportsTo: 'Maria',
    LineManager: 0,
    Rank: 7
  },
  {
    Name: 'William',
    ReportsTo: 'Maria',
    LineManager: 0,
    Rank: 6
  },
  {
    Name: 'Tomas',
    ReportsTo: 'Peter',
    LineManager: 0,
    Rank: 6
  },
  {
    Name: 'Vicky',
    ReportsTo: 'Maria',
    LineManager: 0,
    Rank: 6
  }
]

const lineManager = data.find(({ ReportsTo }) => ReportsTo === '').Name

const rest = data.filter(item => (item.ReportsTo === lineManager))

const min = Math.min.apply( Math, rest.map(({ Rank }) => Rank ))

const final = data.map(item => {

  if (item.Rank > min && item.ReportsTo !== '' && item.ReportsTo === lineManager) {
    return {
      ...item,
      LineManager: 1
    }
  } else {
    return {
      ...item
    }
  }
})

console.log(final)

但是我怎样才能做到更深层次呢?例如 FionaMaria 报告并且 Rank 为 7,而 William 为 6。理想情况下 Fiona 也将有 LineManager: 1

期望的输出是这样的,但顺序不相关

[ 
  { Name: 'Peter', ReportsTo: '', LineManager: '', Rank: 4 }, 
  { Name: 'Tom', ReportsTo: 'Peter', LineManager: 0, Rank: 5 }, 
  { Name: 'Adam', ReportsTo: 'Peter', LineManager: 0, Rank: 5 }, 
  { Name: 'Maria', ReportsTo: 'Peter', LineManager: 0, Rank: 5 },
  { Name: 'Tomas', ReportsTo: 'Peter', LineManager: 1, Rank: 6 },  
  { Name: 'William', ReportsTo: 'Maria', LineManager: 0, Rank: 6 }, 
  { Name: 'Vicky', ReportsTo: 'Maria', LineManager: 0, Rank: 6 },
  { Name: 'Fiona', ReportsTo: 'Maria', LineManager: 1, Rank: 7 } 
] 

如果 Rank 不是组的最小值,您可以收集所有 ReportsTo 组的最小等级并映射具有增量值的新对象。

const
    data = [{ Name: 'Peter', ReportsTo: '', LineManager: '', Rank: 4 }, { Name: 'Tom', ReportsTo: 'Peter', LineManager: 0, Rank: 5 }, { Name: 'Adam', ReportsTo: 'Peter', LineManager: 0, Rank: 5 }, { Name: 'Maria', ReportsTo: 'Peter', LineManager: 0, Rank: 5 }, { Name: 'Fiona', ReportsTo: 'Maria', LineManager: 0, Rank: 7 }, { Name: 'William', ReportsTo: 'Maria', LineManager: 0, Rank: 6 }, { Name: 'Tomas', ReportsTo: 'Peter', LineManager: 0, Rank: 6 }, { Name: 'Vicky', ReportsTo: 'Maria', LineManager: 0, Rank: 6 }],
    temp = data.reduce((r, o) => {
        r[o.ReportsTo] ??= Number.POSITIVE_INFINITY;
        if (r[o.ReportsTo] > o.Rank) r[o.ReportsTo] = o.Rank;
        return r;
    }, {}),
    result = data.map(o => ({ ...o, LineManager: temp[o.ReportsTo] === o.Rank
        ? o.LineManager
        : o.LineManager + 1
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }