How to remove an array element in TypeScript

2 Answers

0 votes
let arr: Array<string> = ['TypeScript', 'C', 'C++', 'Java', 'Python', 'PHP']; 
  
const index = 3;
 
arr.splice(index, 1);
 
console.log(arr); 
 
   
   
   
   
/*
   
run:
   
["TypeScript", "C", "C++", "Python", "PHP"] 
  
*/

 



answered Oct 23, 2021 by avibootz
0 votes
let arr: Array<string> = ['TypeScript', 'C', 'C++', 'Java', 'Python', 'PHP']; 
  
const index = arr.indexOf('Python');

if (index > -1) {
   arr.splice(index, 1);
}
 
console.log(arr); 
 
   
   
   
   
/*
   
run:
   
["TypeScript", "C", "C++", "Java", "PHP"] 
  
*/

 



answered Oct 23, 2021 by avibootz
...