I have the following code in my React app that creates tags out of JSON data:
const files = Object.values(gist.files); const tags = files.map((file) => { let tag = file.language ? file.language : 'Plain Text'; return tag; }); let unique = [...new Set(tags)]; const unique_tags = unique.map((tag) => { return <Tag value={tag}/>; });
The code starts by creating an array out of the files' JSON object, next it maps through the files array and returns the value of the language key (Javascript, C, etc.) and if its not undefined, returns plaintext. After that, it filters so there are only unique tags. Finally, it maps through the unique tags and returns a React component.
As you can see, there are two maps and one filter. I feel like this could be done more efficiently.
The JSON is coming from the GitHub Gist API.
For example: https://api.github.com/gists/25c75fa76cf151568d12