How to change the position of an array element in Node.js

1 Answer

0 votes
const arr = ['node.js', 'php', 'python', 'c', 'c#', 'java'];
  
const fromIndex = arr.indexOf('c#'); 
const toIndex = 2;

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

arr.splice(toIndex, 0, element);
  
console.log(arr); 
  
  
  
  
  
/*
run:
  
[ 'node.js', 'php', 'c#', 'python', 'c', 'java' ]
  
*/

 



answered Jul 6, 2022 by avibootz

Related questions

...