0

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

JSFIDDLE

1 Answer 1

1

You can use Array.find, to find 1st item from array.

let gridData = [{ "title": "Contracts Submitted", "name": "Name" }, { "title": "Your latest invoice is now ready to view", "name": "Ebill" }, { "title": "Meter read reminder", "name": "MeterReadReminder" }, { "title": "Your latest invoice is now ready to view", "name": "Ebill" },];

const filteredItems = [...new Set(gridData.map(item => item.name))];

let results = filteredItems.map((value) => ({
    value,
    display: gridData.find(item => item.name == value)["title"]
}))

console.log(results);

You can optimize performance by a lot with lookup, something like this:

let gridData = [{ "title": "Contracts Submitted", "name": "Name" }, { "title": "Your latest invoice is now ready to view", "name": "Ebill" }, { "title": "Meter read reminder", "name": "MeterReadReminder" }, { "title": "Your latest invoice is now ready to view", "name": "Ebill" },],
    result = [], set = new Set;

for (const { name, title } of gridData) {
    if (set.has(name)) continue;
    result.push({ value: name, display: title });
    set.add(name)
}
console.log(result);

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

3 Comments

Thank you! These both look very promising, I will test it out soon. Ta
Yep both work, trying to work out which one I prefer, I think option 1 as I understand it better. Thanks so much for your time (I would still love this to be returned from a single const or function but I don't know if that is possible).
@lharby, Use 2nd one, which is 100x time faster then 1st approach, if gridData is large, it would be more then 1000x time faster!

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.