here is another old working "nijia" for array correction
var arr = [2,1,3,5,4,0] arr[1] = arr.splice(0,1, arr[1])[0] //console.log(arr)
arrays can be corrected through a method, for understanding by providing the code with comments.
function correctArr(_arr, _param){ /* ΠΊΠΎΡΡΠ΅ΠΊΡΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΌΠ°ΡΡΠΈΠ²Π° ΠΏΠΎ ΠΏΠ°ΡΠ΅ ΠΈΠ½Π΄Π΅ΠΊΡΠ° * _arr -- ΠΌΠ°ΡΡΠΈΠ² ΡΡΠ΅Π±ΡΡΡΠΈΠΉ ΠΊΠΎΡΡΠ΅ΠΊΡΠΈΠΈ * _param -- ΠΏΠ°ΡΠ° [n1,n2] -- ΠΈΠ½Π΄Π΅ΠΊΡΡ ΠΌΠ°ΡΡΠΈΠ²Π° Π΄Π»Ρ Π²Π·Π°ΠΈΠΌΠ½ΠΎΠΉ ΠΏΠ΅ΡΠ΅ΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ */ _arr[_param[1]] = _arr.splice(_param[0],1, _arr[_param[1]])[0] } //correctArr(arr, [1,0]) //console.log(arr.toString())
You can write a method with a loop with function reference, and returning a new array.
function corrects_Arr(){ var _param, _arr = arguments[0].slice(), _arguments = Array.prototype.slice.call(arguments, 1) for(var i = 0; i < _arguments.length; i++){ correctArr(_arr, _arguments[i]) } return _arr } /* console.log( arr.toString() +" "+ "ΠΈΠ½ΡΠΏΠ΅ΠΊΡΠΈΡΡΠ΅ΠΌΡΠΉ ΠΌΠ°ΡΡΠΈΠ² ΠΎΡΡΠ°Π»ΡΡ ΠΏΡΠ΅ΠΆΠ½ΠΈΠΌ" +"\n"+ corrects_Arr(arr, [0,1], [4,3]).toString() +" "+ "Π²Π΅ΡΠ½ΡΠ»ΠΈ ΡΠΊΠΎΡΡΠ΅ΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ²" ) */
for simplicity of future handling of arrays, it is possible to weight the solution of the problem, by assigning the prototype Array constructor to its own method, which can be called on all arrays.
Array.prototype["myCorrects"] = function(_param){ if(_param){ this[_param[1]] = this.splice(_param[0],1, this[_param[1]])[0] this.constructor.prototype.myCorrects.apply(this, Array.prototype.slice.call(arguments, 1)) } return this } arr = arr.myCorrects([0,1], [3,4]).splice(-1,1).concat(arr) console.log(arr.toString())