【问题标题】:Javascript es6 - How to remove duplicates in an array of objects, except the last duplicate one?Javascript es6 - 如何删除对象数组中的重复项,除了最后一个重复项?
【发布时间】:2018-03-09 08:20:35
【问题描述】:

我有一个数组:

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

我想删除价格相同的重复项,只保留最后一个重复项,然后根据价格从高到低对数组进行排序。这是我想要的结果:

var result = [
  {price: 10, amount: 20},
  {price : 7, amount: 15},
  {price: 5, amount: 100},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

【问题讨论】:

  • 但您的结果中没有重复的...

标签: javascript typescript ecmascript-6


【解决方案1】:

使用reduce首先将其转换为对象以删除重复项,最后一个副本应覆盖前一个

var obj = arr.reduce( ( acc, c ) =>  Object.assign(acc, {[c.price]:c.amount}) , {});

将其转换回数组并排序

var output = Object.keys( obj )
              .map( s => ({ price : s, amount : obj[ s ] }) )
              .sort( ( a, b )  => b.price - a.price );

演示

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
];
var obj = arr.reduce( ( acc, c ) =>  Object.assign(acc, {[c.price]:c.amount}) , {});
var output = Object.keys( obj )
              .map( s => ({ price : s, amount : obj[ s ] }) )
              .sort( ( a, b )  => b.price - a.price );
console.log( output );

【讨论】:

  • 这对我来说似乎非常低效,因为它创建了许多不必要的中间对象和调用,并且需要知道输入对象的整个结构而不仅仅是价格属性。使用 reduceRight,可以从原始数组中拼接出副本,只创建一个附加对象作为价格索引。然后可以对缩减后的数组进行排序。
  • @RobG 所以,为了提高效率,您决定改变数组?
  • OP 说“…我想删除重复项”而不是“我想复制数组及其所有值(对象),然后构建另一个经过某些过滤的数组价值观”,这就是它的作用。 ;-)
【解决方案2】:

我会使用 reduceRightsplice 来删除重复项。它不会创建任何无用的中间对象,只会创建一个沿途发现的唯一价格列表:

var arr = [
  {price: 5, amount: 100},
  {price: 3, amount: 50},
  {price: 10, amount: 20},
  {price: 3, amount: 75},
  {price: 7, amount: 15},
  {price: 3, amount: 65},
  {price: 2, amount: 34}
]

arr.reduceRight((acc, obj, i) => {
  acc[obj.price]? arr.splice(i, 1) : acc[obj.price] = true;
  return acc;
}, Object.create(null));

arr.sort((a, b) => b.price - a.price);

console.log(arr)

【讨论】:

    【解决方案3】:

    您可以使用Array.reduce 将结果聚合到一个数组中:

    var arr = [
      {price: 5, amount: 100},
      {price: 3, amount: 50},
      {price: 10, amount: 20},
      {price: 3, amount: 75},
      {price: 7, amount: 15},
      {price: 3, amount: 65},
      {price: 2, amount: 34}
    ]
    
    var results = arr.reduce<{ [price: string] : typeof arr[0] }>((p, e)=> {
        p[e.price] = e
        return p;
    }, {});
    
    var resultsAsArray = Object.keys(results)
         .map(k=>results[k])
         .sort((a, b) => b.price - a.price);
    

    如果定义了数组项的类型,您可以将typeof arr[0] 替换为数组项的类型。

    解决方案的思想是将结果累积到一个以价格为键的对象中,如果多次遇到相同的键,则旧值将被覆盖到最后你将只有最后一个值给定价格。

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 1970-01-01
      • 2016-07-02
      • 2018-08-15
      • 2019-07-05
      • 2020-10-25
      • 2023-03-31
      • 2012-11-24
      • 2021-06-19
      相关资源
      最近更新 更多