How to merge two arrays without duplicate items in Node.js

2 Answers

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

 



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

 



answered Jan 24, 2022 by avibootz

Related questions

1 answer 133 views
1 answer 125 views
2 answers 301 views
1 answer 221 views
1 answer 118 views
1 answer 159 views
...