How to merge two arrays without duplicate items in TypeScript

2 Answers

0 votes
let array1 = ['typescript', 'php', 'c'];
let array2 = ['php', 'typescript', 'c#', 'c++', 'c'];
 
let arr = array1.concat(array2); 
 
arr = arr.filter(function (item, index) {
  return arr.indexOf(item) === index;
});
 
console.log(arr);   
 
  
   
     
     
/*
run:
     
["typescript", "php", "c", "c#", "c++"] 
     
*/

 



answered Jan 24, 2022 by avibootz
0 votes
let array1 = ['typescript', 'php', 'c'];
let array2 = ['php', 'typescript', 'c#', 'c++', 'c'];
 
let arr = [...new Set([...array1,...array2])]
 
console.log(arr);   
 
  
   
     
     
/*
run:
     
["typescript", "php", "c", "c#", "c++"] 
     
*/

 



answered Jan 24, 2022 by avibootz

Related questions

1 answer 135 views
1 answer 127 views
2 answers 321 views
1 answer 203 views
1 answer 119 views
1 answer 191 views
...