除去基于一个键的重复,并采取在阵列的另一个关键的最低值

时间:2019-02-02 20:55:43

标签: javascript arrays sorting object duplicates

如何我排序通过在对象的阵列的另一个关键一个键和重复数据删除可以吗?

在下面的示例中,在同一iD下多次出现同一foo: mov rdx, QWORD PTR d[rip+32] mov rax, QWORD PTR d[rip+16] movdqu xmm0, XMMWORD PTR [rdx] movups XMMWORD PTR [rax], xmm0 mov rdx, QWORD PTR [rdx+16] mov QWORD PTR [rax+16], rdx ret foo1: mov rdx, QWORD PTR f[rip+32] mov rax, QWORD PTR f[rip+16] movdqu xmm0, XMMWORD PTR [rdx] movups XMMWORD PTR [rax], xmm0 movdqu xmm0, XMMWORD PTR [rdx+16] movups XMMWORD PTR [rax+16], xmm0 movdqu xmm0, XMMWORD PTR [rdx+32] movups XMMWORD PTR [rax+32], xmm0 movdqu xmm0, XMMWORD PTR [rdx+48] movups XMMWORD PTR [rax+48], xmm0 movdqu xmm0, XMMWORD PTR [rdx+64] movups XMMWORD PTR [rax+64], xmm0 movdqu xmm0, XMMWORD PTR [rdx+80] movups XMMWORD PTR [rax+80], xmm0 movdqu xmm0, XMMWORD PTR [rdx+96] movups XMMWORD PTR [rax+96], xmm0 movdqu xmm0, XMMWORD PTR [rdx+112] movups XMMWORD PTR [rax+112], xmm0 movdqu xmm0, XMMWORD PTR [rdx+128] movups XMMWORD PTR [rax+128], xmm0 movdqu xmm0, XMMWORD PTR [rdx+144] movups XMMWORD PTR [rax+144], xmm0 movdqu xmm0, XMMWORD PTR [rdx+160] movups XMMWORD PTR [rax+160], xmm0 movdqu xmm0, XMMWORD PTR [rdx+176] movups XMMWORD PTR [rax+176], xmm0 movdqu xmm0, XMMWORD PTR [rdx+192] movups XMMWORD PTR [rax+192], xmm0 ret foo2: mov rsi, QWORD PTR n[rip+32] mov ecx, 138 mov rdi, QWORD PTR n[rip+16] rep movsq ret 。如何为给定ID选择最小距离?

dist

预期输出为

[ { dist: 1.2152494565059755, id: '37000' },
  { dist: 2.168068558345124, id: '9000' },
  { dist: 4.861213457464137, id: '37000' },
  { dist: 5.226238485876963, id: '04000' },
  { dist: 5.278968876845613, id: '29000' },
  { dist: 8.446987036894901, id: '09000' },
  { dist: 8.770432584510608, id: '09000' },
  { dist: 9.848816041209018, id: '04000' },
  { dist: 10.681310440202585, id: '58000' },
  { dist: 11.170746649119321, id: '58000' },
  { dist: 12.84374942857388, id: '37000' },
  { dist: 13.476922171827615, id: '09000' },
  { dist: 14.159308062885033, id: '09000' },
  { dist: 15.117353720958263, id: '80000' },
  { dist: 16.528082434902654, id: '58000' },
  { dist: 17.603866532406027, id: '80000' },
  { dist: 18.49748912990931, id: '37000' } ]

1 个答案:

答案 0 :(得分:1)

您可以使用reduceObject.values来实现它,如下所示:

var array = [{dist:1.2152494565059755,id:'37000'},{dist:2.168068558345124,id:'9000'},{dist:4.861213457464137,id:'37000'},{dist:5.226238485876963,id:'04000'},{dist:5.278968876845613,id:'29000'},{dist:8.446987036894901,id:'09000'},{dist:8.770432584510608,id:'09000'},{dist:9.848816041209018,id:'04000'},{dist:10.681310440202585,id:'58000'},{dist:11.170746649119321,id:'58000'},{dist:12.84374942857388,id:'37000'},{dist:13.476922171827615,id:'09000'},{dist:14.159308062885033,id:'09000'},{dist:15.117353720958263,id:'80000'},{dist:16.528082434902654,id:'58000'},{dist:17.603866532406027,id:'80000'},{dist:18.49748912990931,id:'37000'}]
  
const merged = array.reduce((acc,a) =>{
  const id = parseInt(a.id);
  if(!acc[id] || acc[id]["dist"] > a.dist)
    acc[id] = a;
    
  return acc;
},{})

const final = Object.values(merged).sort((a,b) => a.dist - b.dist)

console.log(final)

创建与蓄能器的每个唯一id如密钥和与该对象最小dist如像该值

{
   "37000": {
      "dist": 1.2152494565059755,
      "id": "37000"
   },
   "9000": {
      "dist": 2.168068558345124,
      "id": "9000"
   },
   .... and so on
}

每次循环浏览时,请检查id是否已经存在或累加器中dist的值是否大于当前项目的dist。如果是,则更新累加器。 (parseInt()是必需的,因为 “09000” 和 “9000” 被认为是相同的id

然后使用Object.values将累加器对象的值转换为数组,然后最后根据sort值对其进行dist

更新

如果Object.values isn't supported yet,则可以map覆盖Object.keys并获得如下值:

const final = Object.keys(merged).map(k => merged[k]).sort((a,b) => a.dist - b.dist)