簡體   English   中英

如何從復雜的對象數組中獲取唯一的對象數組?

[英]How to get unique an array of objects back from a complex an array of objects?

如何從此輸入 JSON 數組中的字段input[i].user獲取唯一值

我可以寫一個 for 循環,但想知道是否有更短的方法使用 filter/set 等? 謝謝

 const input = [{ "id": 133557, "user": "bcasey1", "userfullname": "Bertha Casey", "commentTypeId": 2, "annotationPrimaryId": 141614, "comment": "Red color on ravioli is not true, fix", "deleted": false, "historyno": "133557-0", "timestamp": "Tue Apr 24 10:40:42 CDT 2018", "type": "rectangle" }, { "id": 134038, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 1, "annotationPrimaryId": 142286, "comment": "test123", "deleted": false, "historyno": "134038-0", "timestamp": "Mon Jul 8 22:15:18 CDT 2019", "type": "rectangle" }, { "id": 134039, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 2, "annotationPrimaryId": 142287, "comment": "test234", "deleted": false, "historyno": "134039-0", "timestamp": "Mon Jul 8 22:15:35 CDT 2019", "type": "rectangle" }, { "id": 134112, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 3, "annotationPrimaryId": 142361, "comment": "sadasdasd", "deleted": false, "historyno": "134112-0", "timestamp": "Wed Jul 17 13:03:55 CDT 2019", "type": "rectangle" }, { "id": 134112, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142370, "comment": "sadasdasd s", "deleted": false, "historyno": "134112-1", "timestamp": "Wed Jul 17 15:09:48 CDT 2019", "type": "rectangle" }, { "id": 134113, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 4, "annotationPrimaryId": 142362, "comment": "sadasdasd edited #4", "deleted": false, "historyno": "134113-0", "timestamp": "Wed Jul 17 13:16:39 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142363, "comment": "sadasdasd edited #5", "deleted": false, "historyno": "134114-0", "timestamp": "Wed Jul 17 13:20:06 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142365, "comment": "sadasdasd edited #6", "deleted": false, "historyno": "134114-1", "timestamp": "Wed Jul 17 13:36:53 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142366, "comment": "sadasdasd edited #7", "deleted": false, "historyno": "134114-2", "timestamp": "Wed Jul 17 13:46:36 CDT 2019", "type": "rectangle" }, { "id": 134115, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 2, "annotationPrimaryId": 142367, "comment": "eertet", "deleted": false, "historyno": "134115-0", "timestamp": "Wed Jul 17 14:50:03 CDT 2019", "type": "rectangle" }, { "id": 134118, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 3, "annotationPrimaryId": 142371, "comment": "a", "deleted": false, "historyno": "134118-0", "timestamp": "Wed Jul 17 15:09:58 CDT 2019", "type": "rectangle" }]; const expectedOutput = [{ "user": "bcasey1", "userfullname": "Bertha Casey" }, { "user": "admin", "userfullname": "Administrator Administrator", }]; console.log('expectedOutput', expectedOutput);

使用reduce()Object.assign()

 const input = [{"id":133557,"user":"bcasey1","userfullname":"Bertha Casey","commentTypeId":2,"annotationPrimaryId":141614,"comment":"Red color on ravioli is not true, fix","deleted":false,"historyno":"133557-0","timestamp":"Tue Apr 24 10:40:42 CDT 2018","type":"rectangle"},{"id":134038,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":1,"annotationPrimaryId":142286,"comment":"test123","deleted":false,"historyno":"134038-0","timestamp":"Mon Jul 8 22:15:18 CDT 2019","type":"rectangle"},{"id":134039,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":2,"annotationPrimaryId":142287,"comment":"test234","deleted":false,"historyno":"134039-0","timestamp":"Mon Jul 8 22:15:35 CDT 2019","type":"rectangle"},{"id":134112,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":3,"annotationPrimaryId":142361,"comment":"sadasdasd","deleted":false,"historyno":"134112-0","timestamp":"Wed Jul 17 13:03:55 CDT 2019","type":"rectangle"},{"id":134112,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":0,"annotationPrimaryId":142370,"comment":"sadasdasd s","deleted":false,"historyno":"134112-1","timestamp":"Wed Jul 17 15:09:48 CDT 2019","type":"rectangle"},{"id":134113,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":4,"annotationPrimaryId":142362,"comment":"sadasdasd edited #4","deleted":false,"historyno":"134113-0","timestamp":"Wed Jul 17 13:16:39 CDT 2019","type":"rectangle"},{"id":134114,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":0,"annotationPrimaryId":142363,"comment":"sadasdasd edited #5","deleted":false,"historyno":"134114-0","timestamp":"Wed Jul 17 13:20:06 CDT 2019","type":"rectangle"},{"id":134114,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":0,"annotationPrimaryId":142365,"comment":"sadasdasd edited #6","deleted":false,"historyno":"134114-1","timestamp":"Wed Jul 17 13:36:53 CDT 2019","type":"rectangle"},{"id":134114,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":0,"annotationPrimaryId":142366,"comment":"sadasdasd edited #7","deleted":false,"historyno":"134114-2","timestamp":"Wed Jul 17 13:46:36 CDT 2019","type":"rectangle"},{"id":134115,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":2,"annotationPrimaryId":142367,"comment":"eertet","deleted":false,"historyno":"134115-0","timestamp":"Wed Jul 17 14:50:03 CDT 2019","type":"rectangle"},{"id":134118,"user":"admin","userfullname":"Administrator Administrator","commentTypeId":3,"annotationPrimaryId":142371,"comment":"a","deleted":false,"historyno":"134118-0","timestamp":"Wed Jul 17 15:09:58 CDT 2019","type":"rectangle"}]; const expectedOutput = input.reduce((a, o) => Object.assign(a, { [o.user]: { user: o.user, userfullname: o.userfullname } }), {}); console.log('expectedOutput', Object.values(expectedOutput));

我不太明白你的問題,但也許這對你有用:

let users_id = input.map( d => d.id );

// --> [133557,133558,...]

或者,您可以map到簡化的“user”、“userfullname”對象,然后filter索引與findIndex的結果相同的findIndex ,其中“user”是相等的(JavaScript 數組的唯一方法)

 const input = [{ "id": 133557, "user": "bcasey1", "userfullname": "Bertha Casey", "commentTypeId": 2, "annotationPrimaryId": 141614, "comment": "Red color on ravioli is not true, fix", "deleted": false, "historyno": "133557-0", "timestamp": "Tue Apr 24 10:40:42 CDT 2018", "type": "rectangle" }, { "id": 134038, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 1, "annotationPrimaryId": 142286, "comment": "test123", "deleted": false, "historyno": "134038-0", "timestamp": "Mon Jul 8 22:15:18 CDT 2019", "type": "rectangle" }, { "id": 134039, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 2, "annotationPrimaryId": 142287, "comment": "test234", "deleted": false, "historyno": "134039-0", "timestamp": "Mon Jul 8 22:15:35 CDT 2019", "type": "rectangle" }, { "id": 134112, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 3, "annotationPrimaryId": 142361, "comment": "sadasdasd", "deleted": false, "historyno": "134112-0", "timestamp": "Wed Jul 17 13:03:55 CDT 2019", "type": "rectangle" }, { "id": 134112, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142370, "comment": "sadasdasd s", "deleted": false, "historyno": "134112-1", "timestamp": "Wed Jul 17 15:09:48 CDT 2019", "type": "rectangle" }, { "id": 134113, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 4, "annotationPrimaryId": 142362, "comment": "sadasdasd edited #4", "deleted": false, "historyno": "134113-0", "timestamp": "Wed Jul 17 13:16:39 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142363, "comment": "sadasdasd edited #5", "deleted": false, "historyno": "134114-0", "timestamp": "Wed Jul 17 13:20:06 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142365, "comment": "sadasdasd edited #6", "deleted": false, "historyno": "134114-1", "timestamp": "Wed Jul 17 13:36:53 CDT 2019", "type": "rectangle" }, { "id": 134114, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 0, "annotationPrimaryId": 142366, "comment": "sadasdasd edited #7", "deleted": false, "historyno": "134114-2", "timestamp": "Wed Jul 17 13:46:36 CDT 2019", "type": "rectangle" }, { "id": 134115, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 2, "annotationPrimaryId": 142367, "comment": "eertet", "deleted": false, "historyno": "134115-0", "timestamp": "Wed Jul 17 14:50:03 CDT 2019", "type": "rectangle" }, { "id": 134118, "user": "admin", "userfullname": "Administrator Administrator", "commentTypeId": 3, "annotationPrimaryId": 142371, "comment": "a", "deleted": false, "historyno": "134118-0", "timestamp": "Wed Jul 17 15:09:58 CDT 2019", "type": "rectangle" }]; const simpleInput = input.map(({ user, userfullname }) => ({ user, userfullname })); const filteredInput = simpleInput.filter((user, i, a) => i == a.findIndex(u => u.user == user.user)); console.log(filteredInput)

您可以組合使用Object.keys().reduce()

const uniqueNames = Object.keys(input.reduce((names, obj) => (names[obj.user] = 1, names), {}));

.reduce()操作創建一個對象,其屬性名稱是來自原始數組中對象的“用戶”屬性值。 Object.keys()調用然后創建這些屬性名稱的數組。

如果你想要“全名”值,你可以用它替換1並跳過Object.keys()調用,或者使用Object.entries()來獲取用戶名和全名的數組:

const usersAndNames = Object.entries(input.reduce((users, obj) => (obj[users] = obj.userfullname, obj), {}));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM