Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Change the Position of an Element in an Array

const arr = ['css', 'js', 'ts'];

const fromIndex = arr.indexOf('css'); //  0
const toIndex = 2;

const element = arr.splice(fromIndex, 1)[0];
console.log(element); // ['css']

arr.splice(toIndex, 0, element);

console.log(arr); //  ['js', 'ts', 'css']
Source by bobbyhadz.com #
 
PREVIOUS NEXT
Tagged: #Change #Position #Element #Array
ADD COMMENT
Topic
Name
2+9 =