从一个键重复的数组中删除对象,而另一个键再重复另一个

时间:2019-03-18 01:01:27

标签: javascript arrays node.js duplicates

我有一个包含对象的数组:

n=1 (main)
n=100 (main)
t executing...
n=1 (thread 1)      # the thread used the "n = 1" init code
n_=100 (thread 1)   # the passed reference, not the thread_local
n=2 (thread 1)      # write to the thread_local
n_=100 (thread 1)   # did not change the passed reference
t executing...DONE
n=100 (main, after t.join())
n=200 (main)
t2 executing...
n=1 (thread 2)
n_=200 (thread 2)
n=2 (thread 2)
n_=200 (thread 2)
t2 executing...DONE
n=200 (main, after t2.join())
---
n=200 (main)        # second execution: old state is reused
n=100 (main)
t executing...
n=1 (thread 1)
n_=100 (thread 1)
n=2 (thread 1)
n_=100 (thread 1)
t executing...DONE
n=100 (main, after t.join())
n=200 (main)
t2 executing...
n=1 (thread 2)
n_=200 (thread 2)
n=2 (thread 2)
n_=200 (thread 2)
t2 executing...DONE
n=200 (main, after t2.join())

如何删除具有较新日期的重复对象并获取新数组,如下所示:

var data=[
    { number: '31907411282', unix_date: '1547012427' },
    { number: '31907411282', unix_date: '1547013214' },
    { number: '31907514691', unix_date: '1547015155' },
    { number: '31907514691', unix_date: '1547015232' },
    { number: '31907514691', unix_date: '1547016134' },
    { number: '31907514691', unix_date: '1547016443' },
    { number: '31907638716', unix_date: '1547017122' },
    { number: '31907644067', unix_date: '1547017381' }
]

我不知道如何完成所有程序。 谢谢

更新

我尝试这种构造:

  1. 首先,我们需要对对象进行分组和排序
  2. 第二步是从分组中获取第一个对象并推送到新数组

代码:

var new_data=[
    { number: '31907411282', unix_date: '1547012427' },
    { number: '31907514691', unix_date: '1547015155' },
    { number: '31907638716', unix_date: '1547017122' },
    { number: '31907644067', unix_date: '1547017381' }
]

也许可以更优雅

更新2

@ miroslav-glamuzina提供更多优雅方式:

var data=[
    { number: '31907411282', unix_date: '1547012427' },
    { number: '31907411282', unix_date: '1547013214' },
    { number: '31907514691', unix_date: '1547019134' },
    { number: '31907514691', unix_date: '1547015232' },
    { number: '31907514691', unix_date: '1547016134' },
    { number: '31907514691', unix_date: '1547016443' },
    { number: '31907638716', unix_date: '1547017122' },
    { number: '31907644067', unix_date: '1547017381' }
]

var new_data=[];
var groupByNumber=groupBy(data, 'number');
for(i in groupByNumber){
    var sort_arr=groupByNumber[i].sort(compare)
    new_data.push(sort_arr[0])
}

console.log(new_data)

function compare(a, b) {
  let comparison = 0;
  if (a.unix_date > b.unix_date) {
    comparison = 1;
  } else if (a.unix_date < b.unix_date) {
    comparison = -1;
  }
  return comparison;
}

function groupBy(xs, key) { //group by key
    return xs.reduce(function(rv, x) {
        (rv[x[key]] = rv[x[key]] || []).push(x);
        return rv;
    }, {});
};

3 个答案:

答案 0 :(得分:2)

您可以使用reduce遍历数组并存储一组键(number)来检查是否存在。然后推入新数组。

var data=[
    { number: '31907411282', unix_date: '1547012427' },
    { number: '31907411282', unix_date: '1547013214' },
    { number: '31907514691', unix_date: '1547015155' },
    { number: '31907514691', unix_date: '1547015232' },
    { number: '31907514691', unix_date: '1547016134' },
    { number: '31907514691', unix_date: '1547016443' },
    { number: '31907638716', unix_date: '1547017122' },
    { number: '31907644067', unix_date: '1547017381' }
]
const rs = data.reduce((acc, e) => {
  if(!acc.number.has(e.number)) {
    acc.newData.push(e)
    acc.number.add(e.number)
  }
  return acc
}, {number: new Set(), newData: []})

console.log(rs.newData)

答案 1 :(得分:1)

您可以使用reduce来实现:

var data = [{
    number: '31907411282',
    unix_date: '1547013214'
  },
  {
    number: '31907411282',
    unix_date: '1547012427'
  },
  {
    number: '31907514691',
    unix_date: '1547015155'
  },
  {
    number: '31907514691',
    unix_date: '1547015232'
  },
  {
    number: '31907514691',
    unix_date: '1547016134'
  },
  {
    number: '31907514691',
    unix_date: '1547016443'
  },
  {
    number: '31907638716',
    unix_date: '1547017122'
  },
  {
    number: '31907644067',
    unix_date: '1547017381'
  }
];

let filtered = data.reduce((acc, item) => {
  if (!acc.some((e, i) => {
      if (item.number === e.number) {
        if (item.unix_date < e.unix_date) {
          acc.splice(i, 1, item);
        }
        return true;
      }
    })) {
    acc.push(item);
  }
  return acc;
}, []);

console.log(filtered);

希望这会有所帮助,

答案 2 :(得分:1)

按日期对项目进行排序,减少数组,仅保留具有number的元素,该元素在累加器数组中不存在。

let data = [{
      number: '31907411282',
      unix_date: '1547013214'
    },
    {
      number: '31907411282',
      unix_date: '1547012427'
    },
    {
      number: '31907514691',
      unix_date: '1547015155'
    },
    {
      number: '31907514691',
      unix_date: '1547015232'
    },
    {
      number: '31907514691',
      unix_date: '1547016134'
    },
    {
      number: '31907514691',
      unix_date: '1547016443'
    },
    {
      number: '31907638716',
      unix_date: '1547017122'
    },
    {
      number: '31907644067',
      unix_date: '1547017381'
    }
    ];

    let filtered = data
      .sort((a, b) => (a.unix_date > b.unix_date) ? 1 : -1)
      .reduce((acc, item) => {
        if (!acc.find(el => el.number === item.number)) {
          acc.push(item)
        }
        return acc
      }, []);

      console.log(filtered);