- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我使用 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/
我有数组: [ [ '245', '0', '0.0100', '245,0,0.01' ], [ '245', '1', '0.0100', '245,1,0.01' ], [ '208',
const uniqBy = (arr, fn) => { let res = [] const hash = new Set() for (let i = 0; i arr.filte
const data = [ {id: "001", failedReason: [1000]}, {id: "001", failedReason: [1001]}, {id: "001", fai
JSFiddle Example let obj = [{'vote':999,'name':'D'}, {'vote':341,'name':'A'},
在这段代码中,我被困在了 _.uniqBy(array,iteratee),this iteratee 可以是函数,也可以是字符串 在哪里放置条件以检查属性的唯一性,因为 itratee 函数可以是任
这是我使用 lodash 的代码 console.log('stackList2', stackList2); console.log('stackList2.length', stackList2.
我是一名优秀的程序员,十分优秀!