I am using [...new Set] to create a new array of unique items, the issue I'm facing is that the ultimate goal is to create a new array of objects which should be unique (or at least unique according to the first property).
Some sample data:
const props = {};
props.gridData = [
{
"id": 198,
"dateReceived": "2020-12-29T10:10:45.513",
"title": "Contracts Submitted",
"subject": "Contract(s) for xxx Ltd submitted.",
"readUnread": false,
"userId": 13,
"emailAddress": "[email protected]",
"organisation": 43,
"callToAction": "string",
"json": "<h1>Your latest statement is here</h1>",
"name": "Name"
},
{
"id": 337,
"dateReceived": "2021-04-09T11:41:03.033",
"title": "Your latest invoice is now ready to view",
"subject": "Your latest invoice is here (Account ID XXXXXX)",
"readUnread": true,
"userId": 13,
"emailAddress": "[email protected]",
"organisation": 43,
"callToAction": null,
"json": "<h1>Your latest invoice is here</h1>",
"name": "Ebill"
},
{
"id": 343,
"dateReceived": "2021-04-09T11:41:03.047",
"title": "Meter read reminder",
"subject": "6,Unit 1, Somewhere, Somewhere Else, AB1 1YZ",
"readUnread": true,
"userId": 13,
"emailAddress": "[email protected]",
"organisation": 43,
"callToAction": null,
"json": "<h1>Your latest statement is here</h1>",
"name": "MeterReadReminder"
},
{
"id": 387,
"dateReceived": "2021-05-07T07:26:40.653",
"title": "Your latest invoice is now ready to view",
"subject": "Your latest invoice is here (Account ID XXXXXX)",
"readUnread": false,
"userId": 13,
"emailAddress": "[email protected]",
"organisation": 43,
"callToAction": "NULL",
"json": "<h1>Your latest invoice is here</h1>",
"name": "Ebill"
},
...
]
Here is my current JS:
const filteredItems = () => [...new Set(props.gridData.map(item =>
item.name
))];
This will create a new array of unique items. The goal is go have filteredItems look like this:
const filteredItems = [
{ value: 'MeterReadReminder', display: 'Meter Read Reminder' },
{ value: 'Ebill', display: 'Your latest invoice is now ready to view' },
...
];
Obviously value and display would be read from the map function so 'value': item.name etc.
I don't know if this can be achieved in a single function or whether I need to create two arrays sequentially. Also sorry if this question title is poorly worded, I will try and tighten that up.
(I am using React and TS, but the jsfiddle is pure js).