下面是我使用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只包含两个唯一ids为5a745c25d8e58a4dddafcd66和5a745c25d8e58a4dddafcd68的对象
谢谢
发布于 2018-02-02 21:16:18
起作用了。_id和Expire值应为字符串,否则无效。
> 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' ] } ]https://stackoverflow.com/questions/48583237
复制相似问题