I'm trying to remove duplicates in the array. I have tags from posts, which is also array:
tags: ['react', 'javascript', 'node']
and from map function I have output like this:
node react javascript react javascript react javascript node
<div class="tags">
{posts.map(post =>
post.frontmatter.tags.map(tag => (
<Link
key={tag + `tag`}
to={`/tags/${kebabCase(tag)}/`}
className="tag is light"
>
{tag}
</Link>
))
)}
</div>
I've tried to implement .filter and reduce, but I'm stuck.
Thanks in advance
You can use a Set to obtain the unique elements from the array and use spread syntax to get the result as an array.
const arr = ["node", "react", "javascript", "react", "javascript", "react", "javascript", "node"];
const res = [...new Set(arr)];
console.log(res);
var sandwiches = ['turkey', 'ham', 'turkey', 'tuna', 'pb&j', 'ham', 'turkey', 'tuna'];
var deduped = Array.from(new Set(sandwiches));
// Logs ["turkey", "ham", "tuna", "pb&j"]
console.log(deduped);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With