2

How can I get unique values from this input JSON array on field input[i].user

I can write a for loop but wanted to know if there is a shorter way using filter/set etc ? Thanks

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

5
  • 2
    It's not a "JSON array"; it's an array of objects. Commented Jul 18, 2019 at 15:11
  • 1
    There is no attempt. You are just showing an input and an output. Please put in some work. Commented Jul 18, 2019 at 15:16
  • @Pointy edited thanks Commented Jul 18, 2019 at 15:18
  • The question is missing an important detail: what should happen when there are different "full name" entries for the same "user" value? Commented Jul 18, 2019 at 15:18
  • @Pointy please read carefully this input JSON array on field input[i].user apologies for my communication skills Commented Jul 18, 2019 at 15:19

4 Answers 4

4

Using reduce() and 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));

Sign up to request clarification or add additional context in comments.

2 Comments

Why call Object.assign()? Why not just set a[o.user] to a value?
@Pointy that looks cleaner i think
2

Alternative, you could map to the simplified "user", "userfullname" object then filter on where the index is the same as the result of findIndex where "user" is equal (a way to unique for javascript arrays)

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)

1 Comment

I am going to accept your answer as it is faster than @User863's answer jsfiddle.net/dota2pro/54sg1rtf/1
2

I dont understand well your question, but maybe this works for you:

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

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

1 Comment

Sorry, i dont want just a list of userids
0

You can use a combination of Object.keys() and .reduce():

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

The .reduce() operation creates an object whose property names are the "user" property values from the objects in the original array. The Object.keys() call then creates an array of those property names.

If you want the "full name" values, you can replace 1 by that and skip the Object.keys() call, or use Object.entries() to get an array of user and full names:

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

2 Comments

@dota2pro well look at the answers, see what they do, and then adapt the code to your precise requirements.
still not correct, please see the last element of the array jsfiddle.net/dota2pro/bhow1Lmy/4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.