lodash uniqBy不是在id领域的统一

时间:2018-02-02 12:58:16

标签: node.js lodash

这是我使用lodash的代码

console.log('stackList2', stackList2);
console.log('stackList2.length', stackList2.length);
var stackList3 = _.uniqBy(stackList2, '_id');
console.log('stackList3', stackList3);

这是输出

stackList2 [ { _id: 5a745c25d8e58a4dddafcd66,
               entities: [ '1tb', 'memory' ],
               keyPhrases: [ 'a usb memory stick', 'cheap' ],
               expiry: 2026-02-02T12:40:05.727Z,
               tags: [ 'memory' ] },
            { _id: 5a745c25d8e58a4dddafcd68,
              entities: [ '3tb', 'pankaj' ],
              keyPhrases: [ 'stick', 'pkpk' ],
              expiry: 2026-02-02T12:40:05.732Z,
              tags: [ 'memory' ] },
            { _id: 5a745c25d8e58a4dddafcd66,
              entities: [ '1tb', 'memory' ],
              keyPhrases: [ 'a usb memory stick', 'cheap' ],
              expiry: 2026-02-02T12:40:05.727Z,
              tags: [ 'memory' ] },
            { _id: 5a745c25d8e58a4dddafcd68,
              entities: [ '3tb', 'pankaj' ],
              keyPhrases: [ 'stick', 'pkpk' ],
              expiry: 2026-02-02T12:40:05.732Z,
              tags: [ 'memory' ] } ]
stackList2.length 4
stackList3 [ { _id: 5a745c25d8e58a4dddafcd66,
               entities: [ '1tb', 'memory' ],
               keyPhrases: [ 'a usb memory stick', 'cheap' ],
               expiry: 2026-02-02T12:40:05.727Z,
               tags: [ 'memory' ] },
             { _id: 5a745c25d8e58a4dddafcd68,
               entities: [ '3tb', 'pankaj' ],
               keyPhrases: [ 'stick', 'pkpk' ],
               expiry: 2026-02-02T12:40:05.732Z,
               tags: [ 'memory' ] },
             { _id: 5a745c25d8e58a4dddafcd66,
               entities: [ '1tb', 'memory' ],
               keyPhrases: [ 'a usb memory stick', 'cheap' ],
               expiry: 2026-02-02T12:40:05.727Z,
               tags: [ 'memory' ] },
             { _id: 5a745c25d8e58a4dddafcd68,
               entities: [ '3tb', 'pankaj' ],
               keyPhrases: [ 'stick', 'pkpk' ],
               expiry: 2026-02-02T12:40:05.732Z,
               tags: [ 'memory' ] } ]

如您所见,stackList2和stackList3完全相同。我期待stackList3只包含两个具有唯一ID的对象5a745c25d8e58a4dddafcd66和5a745c25d8e58a4dddafcd68

由于

1 个答案:

答案 0 :(得分:0)

它正在运作。 _id和expiry应该是字符串,否则它将无效。

> list = [
...     {
...         _id: '5a745c25d8e58a4dddafcd66',
...         entities: ['1tb', 'memory'],
...         keyPhrases: ['a usb memory stick', 'cheap'],
...         expiry: '2026-02-02T12:40:05.727Z',
...         tags: ['memory']
...     },
...     {
...         _id: '5a745c25d8e58a4dddafcd68',
...         entities: ['3tb', 'pankaj'],
...         keyPhrases: ['stick', 'pkpk'],
...         expiry: '2026-02-02T12:40:05.732Z',
...         tags: ['memory']
...     },
...     {
...         _id: '5a745c25d8e58a4dddafcd66',
...         entities: ['1tb', 'memory'],
...         keyPhrases: ['a usb memory stick', 'cheap'],
...         expiry: '2026-02-02T12:40:05.727Z',
...         tags: ['memory']
...     },
...     {
...         _id: '5a745c25d8e58a4dddafcd68',
...         entities: ['3tb', 'pankaj'],
...         keyPhrases: ['stick', 'pkpk'],
...         expiry: '2026-02-02T12:40:05.732Z',
...         tags: ['memory']
...     }
... ];
[ { _id: '5a745c25d8e58a4dddafcd66',
    entities: [ '1tb', 'memory' ],
    keyPhrases: [ 'a usb memory stick', 'cheap' ],
    expiry: '2026-02-02T12:40:05.727Z',
    tags: [ 'memory' ] },
  { _id: '5a745c25d8e58a4dddafcd68',
    entities: [ '3tb', 'pankaj' ],
    keyPhrases: [ 'stick', 'pkpk' ],
    expiry: '2026-02-02T12:40:05.732Z',
    tags: [ 'memory' ] },
  { _id: '5a745c25d8e58a4dddafcd66',
    entities: [ '1tb', 'memory' ],
    keyPhrases: [ 'a usb memory stick', 'cheap' ],
    expiry: '2026-02-02T12:40:05.727Z',
    tags: [ 'memory' ] },
  { _id: '5a745c25d8e58a4dddafcd68',
    entities: [ '3tb', 'pankaj' ],
    keyPhrases: [ 'stick', 'pkpk' ],
    expiry: '2026-02-02T12:40:05.732Z',
    tags: [ 'memory' ] } ]
> var _ = require('lodash')
> _.uniqBy(list, '_id')
[ { _id: '5a745c25d8e58a4dddafcd66',
    entities: [ '1tb', 'memory' ],
    keyPhrases: [ 'a usb memory stick', 'cheap' ],
    expiry: '2026-02-02T12:40:05.727Z',
    tags: [ 'memory' ] },
  { _id: '5a745c25d8e58a4dddafcd68',
    entities: [ '3tb', 'pankaj' ],
    keyPhrases: [ 'stick', 'pkpk' ],
    expiry: '2026-02-02T12:40:05.732Z',
    tags: [ 'memory' ] } ]