Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Duplicates within a map function [duplicate]

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

like image 343
BloodOverdrive Avatar asked Jun 11 '26 23:06

BloodOverdrive


2 Answers

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);
like image 57
Unmitigated Avatar answered Jun 14 '26 12:06

Unmitigated


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);
like image 34
Avni Onur Pehlivan Avatar answered Jun 14 '26 11:06

Avni Onur Pehlivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!