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

dota2pro

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

William Lohan

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)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to multi filter an array of objects?

分類Dev

How to remove duplicates objects from array in javascript?

分類Dev

How to get an array of counts of specific objects in an existing array?

分類Dev

How to get a particular attribute from an array of array objects?

分類Dev

Getting array of objects from CodableAlamofire

分類Dev

Get average value from array consisting of objects based on objects fields

分類Dev

get list of unique objects from an arraylist in java

分類Dev

How to get a the sum of multiple arrays within an array of objects?

分類Dev

Find the value inside the array of objects that include children from the array of objects

分類Dev

Processing like filtering ,distinct, transforming array of objects into sequence of objects and back to collection array of objects using RXJS only

分類Dev

How to get an Array of Objects from Firestore in Swift?

分類Dev

Fetch unique values from array of objects based on dynamically passed property

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

How to create an array of objects in JavaScript?

分類Dev

How to extract a property from array of objects and slice it?

分類Dev

How to change json data format from array to array of objects with javascript?

分類Dev

How to put values of objects from an array into a string

分類Dev

How to get only one value in Javascript array of objects using for of and for in statements?

分類Dev

How to update a complex json object based on array of objects

分類Dev

How to get the sum of number from an array of objects, using pure JS?

分類Dev

GSON deserialize an array of complex objects

分類Dev

how to hold objects in array in hadoop

分類Dev

Check meter from array of objects

分類Dev

Get object from array with NSDictionary objects

分類Dev

Android: How to parse JSON Array of Array of objects

分類Dev

Removing an object from an array of objects

分類Dev

How to delegate to an array of objects in JavaScript

分類Dev

How to get all values of objects inside array

分類Dev

How to search for a value in array of objects and get it in Laravel?

Related 関連記事

  1. 1

    How to multi filter an array of objects?

  2. 2

    How to remove duplicates objects from array in javascript?

  3. 3

    How to get an array of counts of specific objects in an existing array?

  4. 4

    How to get a particular attribute from an array of array objects?

  5. 5

    Getting array of objects from CodableAlamofire

  6. 6

    Get average value from array consisting of objects based on objects fields

  7. 7

    get list of unique objects from an arraylist in java

  8. 8

    How to get a the sum of multiple arrays within an array of objects?

  9. 9

    Find the value inside the array of objects that include children from the array of objects

  10. 10

    Processing like filtering ,distinct, transforming array of objects into sequence of objects and back to collection array of objects using RXJS only

  11. 11

    How to get an Array of Objects from Firestore in Swift?

  12. 12

    Fetch unique values from array of objects based on dynamically passed property

  13. 13

    How to get the combination of array values from nested arrays in an array of objects

  14. 14

    How to create an array of objects in JavaScript?

  15. 15

    How to extract a property from array of objects and slice it?

  16. 16

    How to change json data format from array to array of objects with javascript?

  17. 17

    How to put values of objects from an array into a string

  18. 18

    How to get only one value in Javascript array of objects using for of and for in statements?

  19. 19

    How to update a complex json object based on array of objects

  20. 20

    How to get the sum of number from an array of objects, using pure JS?

  21. 21

    GSON deserialize an array of complex objects

  22. 22

    how to hold objects in array in hadoop

  23. 23

    Check meter from array of objects

  24. 24

    Get object from array with NSDictionary objects

  25. 25

    Android: How to parse JSON Array of Array of objects

  26. 26

    Removing an object from an array of objects

  27. 27

    How to delegate to an array of objects in JavaScript

  28. 28

    How to get all values of objects inside array

  29. 29

    How to search for a value in array of objects and get it in Laravel?

ホットタグ

アーカイブ