gpt4 book ai didi

node.js - lodash uniqBy 不是 id 字段上的 uniqing

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:12 26 4
gpt4 key购买 nike

这是我使用 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 的对象

谢谢

最佳答案

它正在工作。 _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' ] } ]

关于node.js - lodash uniqBy 不是 id 字段上的 uniqing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48583237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com