• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

98
Views
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.

over 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

over 3 years ago · Juan Pablo Isaza Report

0

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


const result = mapedTopicsArray
    .reduce((acc, prev) => acc.concat(prev), [])
    .filter(ifThisItemFirstAppearance)

function ifThisItemFirstAppearance(value, index, self) {
    return self.indexOf(value) === index
}

console.log(result)

First reduce the list of lists into a list, then filter repeated items.

Partial credit to this answer on how to filter duplicates

over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error