如何从复杂的对象数组中获得唯一的对象数组?

时间:2019-07-18 15:09:30

标签: javascript ecmascript-6

如何从字段input[i].user的输入JSON数组中获取唯一值

我可以编写一个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);

4 个答案:

答案 0 :(得分:3)

使用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));

答案 1 :(得分:1)

我不太了解您的问题,但这也许对您有用:

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

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

答案 2 :(得分:1)

或者,您可以map到简化的“ user”,“ userfullname”对象,然后filter到索引与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)

答案 3 :(得分:0)

您可以结合使用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), {}));