How to change the position of an array element in TypeScript

1 Answer

0 votes
const arr = ['typescript', 'php', 'python', 'c', 'c#', 'c++'];
   
const fromIndex = arr.indexOf('c'); 
const toIndex = 1;

const element = arr.splice(fromIndex, 1)[0];

arr.splice(toIndex, 0, element);
  
console.log(arr); 
  
  
  
  
  
/*
run:
  
["typescript", "c", "php", "python", "c#", "c++"] 
  
*/

 



answered Jul 6, 2022 by avibootz
edited Jul 6, 2022 by avibootz
...