How to change the position of an array element in JavaScript

1 Answer

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

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

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

 



answered Jul 6, 2022 by avibootz

Related questions

...