-2

Am trying the following

Input:
myarray1=['Tag A','Tag B','Tag C']
myarray2=['Tag B','Tag C']
myarray3=['Tag A','Tag D']

Output:

outputArray=['Tag A','Tag B','Tag C','Tag D']

The output array should conatin the unique values from the input arrays.

Am trying this in typescript but not able to find the unique values from input array. Any suggestions how to achieve this in optimal way

4

4 Answers 4

5

Pass the merged arrays into a Set and spread that set into results array

myarray1=['Tag A','Tag B','Tag C']
myarray2=['Tag B','Tag C']
myarray3=['Tag A','Tag D']

const uniques = [...new Set(myarray1.concat(myarray2,myarray3))]

console.log(uniques)

Sign up to request clarification or add additional context in comments.

Comments

3

You can easily do it with ES6 Spread and Set

myarray1=['Tag A','Tag B','Tag C']
myarray2=['Tag B','Tag C']
myarray3=['Tag A','Tag D']
const uniqueArr = [...new Set([...myarray1,...myarray1,...myarray3])];
console.log(uniqueArr)

2 Comments

where do you use destructuring here?
@quirimmo sorry my bad. Just used wrong wording :(
1

Very simple, only using array functions, first you conctainate all of the arrays, then you use the fitler function to simply ensure that there's only ever unique values.

const myarray1 = ['Tag A', 'Tag B', 'Tag C'],
  myarray2 = ['Tag B', 'Tag C'],
  myarray3 = ['Tag A', 'Tag D'];

const mergedArray = Array.concat(myarray1, myarray2, myarray3).filter((s, i, array) => array.indexOf(s) === i);
console.log(mergedArray);

Comments

1

You can do it with reduce Set and spread operator.

let arr1=['Tag A','Tag B','Tag C']
let arr2=['Tag B','Tag C']
let arr3=['Tag A','Tag D']

let arr = [arr1,arr2,arr3]
let op = [...new Set( arr.reduce((o,c)=>o.concat(c),[]))];

console.log(op);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.