How to prevent adding duplicates to an array in Node.js

1 Answer

0 votes
const arr = ['javascript', 'typescript', 'nodejs', 'python'];

const s = 'c#';

if (!arr.includes(s)) {
  	arr.push(s);
}

console.log(arr); 

   
   
   
   
/*
run:
   
[ 'javascript', 'typescript', 'nodejs', 'python', 'c#' ]
   
*/

 



answered Apr 24, 2022 by avibootz

Related questions

1 answer 196 views
1 answer 190 views
1 answer 118 views
1 answer 123 views
3 answers 341 views
...