Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two arrays In one without duplicates

What is the best aproach using ES6+ sintaxe to go from this:

const mapedTopicsArray = [
                            ['javascript', 'reactjs'],
                            ['Java', 'reactjs'],                   
                         ]

To this:

const topicsArrayMergedWithoutDuplicates = ['javascript', 'reactjs','Java']                   
                         

I know that if I use .reduce() I can acomplish that, but I can't figure out how, the nested Array thing is bogging me.

like image 470
Cristian Müller Avatar asked Nov 18 '25 00:11

Cristian Müller


1 Answers

You can easily achieve the result using Set and flat.

const mapedTopicsArray = [
  ["javascript", "reactjs"],
  ["Java", "reactjs"],
];

const topicsArrayMergedWithoutDuplicates  = [...new Set(mapedTopicsArray.flat())];
console.log(topicsArrayMergedWithoutDuplicates );
like image 140
decpk Avatar answered Nov 20 '25 14:11

decpk



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!