Merge two arrays into a single array without any duplicate elements

Exploring the optimal way to convert this using ES6+ syntax:

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

To achieve this result:

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

I understand that utilizing .reduce() could help in achieving this task, however, the nested Array structure is causing some confusion for me.

Answer №1

To achieve the desired outcome, a combination of Set and flat can be utilized effectively.

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

const mergedTopicsWithoutDuplicates = [...new Set(topicsArr.flat())];
console.log(mergedTopicsWithoutDuplicates );

Answer №2

const combinedTopicsArray = [
                            ['python', 'django'],
                            ['Ruby', 'rails'],                   
                         ]


const uniqueTopics = combinedTopicsArray
    .reduce((accumulator, currentValue) => accumulator.concat(currentValue), [])
    .filter(checkFirstAppearance)

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

console.log(uniqueTopics)

Merge subarrays into a single array and then remove duplicates.

Credit goes to this solution for the duplicate filtering technique

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Looking to navigate to the bottom or shift focus to the bottom in an Angular application? Here's

In the div, I have added an element and I am looking to automatically scroll to the bottom of the div every time a new element is added. While I know how to achieve this using jQuery, I am unsure of how to do it using AngularJS. The id of the div is testC ...

Escaping multiple levels of quotations in a string within HTML documents

Currently, I am developing an application with Java as the backend and AngularJS 1.0 for the frontend. To display data tables, I am utilizing the veasy AngularJS plugin which I have customized extensively for my app. However, I am facing a small issue. I ...

Exploring the Google Plus API: Discover individuals who are following a specific individual

Has anyone managed to successfully extract a list of individuals who have included a specific user in their circles or are following them on social media platforms? I am currently using the official JS Library for this task, but any solution in any progr ...

How to retrieve ancestor route parameters in Angular?

My route structure is as follows: const appRoutes:Routes = [ { path: 'article', component: ArticleComponent, children: [ { path: '', component: ArticleListComponent }, { path: ...

Why is the last move of the previous player not displayed in this game of tic tac toe?

Why does the last move of the previous player not show up in this tic-tac-toe game? When it's the final turn, the square remains empty with neither an "O" nor an "X". What could be causing this issue? Here is the code snippet: The HTML code: <div ...

Is there a correct way to properly duplicate a JSON array in Redux?

As a newcomer to Redux, I found the concepts somewhat confusing at first. For instance, imagine that there is an array in the redux state. const state = [ {show: false, id : '1'}, {show: false, id : '2'}, {show: false, id : &apo ...

Step-by-step guide on how to have an AngularJS controller run continuously every 5 seconds once it has been initially called

My angular js file (app.js) contains the uuidCtrl controller, which triggers when called. When I want to call the controller 2 times, I have to load the page twice. However, I am looking for a solution to run it continuously after the first time it is call ...

How can I make rows appear dynamically when the user clicks a button?

I am trying to add rows dynamically when the user clicks on a button. I have created a script for this purpose, but unfortunately, it is not working as expected. Can someone please assist me with fixing it? <script> var i; i = 2; function AddR ...

Calculate the values for top, left, width, and height in a Three.js projection

I'm currently facing an issue: I am trying to position a DIV on top of a WebGL animation. The scenario involves rotating a `mesh` using a `PlaneGeometry` to fill a rectangular area, and then placing the DIV at the top of that space. To achieve this, I ...

Determine the count of all the positive numbers present in my array

Can someone help me figure out how to count the positive numbers in my code? The output I'm getting is not correct, so please provide a detailed explanation of where my mistake might be. I want to make sure that the output matches the desired result. ...

My presentations are not functioning as expected, could there be a problem with my HTML, CSS, or JavaScript coding?

My website utilizes a Slideshow feature to display images for blog posts. Recently, I attempted to include multiple slideshows within an article, which unfortunately caused all of the slideshows to malfunction. As it stands now, when the code is executed, ...

Learn the steps to access a Collection in Meteor.js and then send it to a function once the DOM has finished

Is there a way to ensure that a query on the collection is executed before running another function when a specific div has been rendered? When attempting the following, an error from .highcharts() is returned indicating that the div #chart cannot be foun ...

Tips for locating and substituting a string in Javascript

Is there a way to locate a particular word within a string and substitute it using JavaScript? For instance Here is a lengthy sentence I want to locate the word "sentence" and modify it to "phrase", resulting in: Here is a lengthy phrase ...

Changing the order of elements in a JavaScript array

Issue: Develop a function that accepts an array as input and outputs a new array where the first and last elements are switched. The initial array will always be at least 2 elements long (for example, [1,5,10,-2] should result in [-2,5,10,1]). Here is ...

JavaScript code failing to return array contents

As I navigate through the realms of NodeJS application development handling .nessus result files, I encounter an intriguing behavior quandary when generating an array of numbers. The primary goal is to create an array [1234,1223,1222] from a "results" file ...

Tips for pulling information from two different services using an identification number and showcasing the data

Looking for a way to select data in AngularJS (JavaScript) similar to PHP's SELECT "message" FROM "users" WHERE id = $id? Here is my current setup: app.service("users", function() { this.userList = [ { userId: 1, u ...

List the @font-face URLs using JavaScript or jQuery

Can JavaScript or JQuery be used to list all the @font-face URLs (web font URLs) on a specific HTML page? ...

What is the best way to persist in asking until getting a specific response?

I am struggling to create code that iterates through a query until the desired result is achieved. Let's say I make this request: {{API host}}/{{APP ID}}/{{REST API}}/hive/Management/set/keys?filterPattern=*&pageSize=5&cursor=0 The response f ...

Utilize jQuery in phantom.js to send an ajax request across domains

I am currently in the process of testing a chrome plugin by emulating a portion of its functionality on phantomjs. My objective for phantom seems quite straightforward, yet I am encountering issues. I aim for it to navigate to a specific webpage and withi ...

How to manage simultaneous requests in Parse Server Cloud Code

Imagine having some Cloud Code running on a Parse Server: Parse.Cloud.beforeSave("UserProfiles", function(request, response) { const query = new Parse.Query("UserProfiles"); query.equalTo("user", request.user); query.count({ success: f ...