Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var arr = [ 'a', 'b', 'c', 'd', 'e'];
- Array.prototype.move = function (old_index, new_index) {
- if (new_index >= this.length) {
- var k = new_index - this.length;
- while ((k--) + 1) {
- this.push(undefined);
- }
- }
- this.splice(new_index, 0, this.splice(old_index, 1)[0]);
- return this; // for testing purposes
- };
- Array.prototype.move = function (old_index, new_index) {
- while (old_index < 0) {
- old_index += this.length;
- }
- while (new_index < 0) {
- new_index += this.length;
- }
- if (new_index >= this.length) {
- var k = new_index - this.length;
- while ((k--) + 1) {
- this.push(undefined);
- }
- }
- this.splice(new_index, 0, this.splice(old_index, 1)[0]);
- return this; // for testing purposes
- };
- Array.prototype.move = function(from, to) {
- this.splice(to, 0, this.splice(from, 1)[0]);
- };
- Array.prototype.move2 = function(pos1, pos2) {
- // local variables
- var i, tmp;
- // cast input parameters to integers
- pos1 = parseInt(pos1, 10);
- pos2 = parseInt(pos2, 10);
- // if positions are different and inside array
- if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
- // save element from position 1
- tmp = this[pos1];
- // move element down and shift other elements up
- if (pos1 < pos2) {
- for (i = pos1; i < pos2; i++) {
- this[i] = this[i + 1];
- }
- }
- // move element up and shift other elements down
- else {
- for (i = pos1; i > pos2; i--) {
- this[i] = this[i - 1];
- }
- }
- // put element from position 1 to destination
- this[pos2] = tmp;
- }
- }
- function arraymove(arr, fromIndex, toIndex) {
- var element = arr[fromIndex];
- arr.splice(fromIndex, 1);
- arr.splice(toIndex, 0, element);
- }
- Array.prototype.move = function(from,to){
- this.splice(to,0,this.splice(from,1)[0]);
- return this;
- };
- var arr = [ 'a', 'b', 'c', 'd', 'e'];
- arr.move(3,1);//["a", "d", "b", "c", "e"]
- var arr = [ 'a', 'b', 'c', 'd', 'e'];
- arr.move(0,2);//["b", "c", "a", "d", "e"]
- alert(arr.move(0,2).join(','));
- function move(array, from, to) {
- if( to === from ) return array;
- var target = array[from];
- var increment = to < from ? -1 : 1;
- for(var k = from; k != to; k += increment){
- array[k] = array[k + increment];
- }
- array[to] = target;
- return array;
- }
- ({}) == ({}); // false
- function moveObjectAtIndex(array, sourceIndex, destIndex) {
- var placeholder = {};
- // remove the object from its initial position and
- // plant the placeholder object in its place to
- // keep the array length constant
- var objectToMove = array.splice(sourceIndex, 1, placeholder)[0];
- // place the object in the desired position
- array.splice(destIndex, 0, objectToMove);
- // take out the temporary object
- array.splice(array.indexOf(placeholder), 1);
- }
- function move(array, oldIndex, newIndex) {
- if (newIndex >= array.length) {
- newIndex = array.length - 1;
- }
- array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
- return array;
- }
- describe('ArrayHelper', function () {
- it('Move right', function () {
- let array = [1, 2, 3];
- arrayHelper.move(array, 0, 1);
- assert.equal(array[0], 2);
- assert.equal(array[1], 1);
- assert.equal(array[2], 3);
- })
- it('Move left', function () {
- let array = [1, 2, 3];
- arrayHelper.move(array, 1, 0);
- assert.equal(array[0], 2);
- assert.equal(array[1], 1);
- assert.equal(array[2], 3);
- });
- it('Move out of bounds to the left', function () {
- let array = [1, 2, 3];
- arrayHelper.move(array, 1, -2);
- assert.equal(array[0], 2);
- assert.equal(array[1], 1);
- assert.equal(array[2], 3);
- });
- it('Move out of bounds to the right', function () {
- let array = [1, 2, 3];
- arrayHelper.move(array, 1, 4);
- assert.equal(array[0], 1);
- assert.equal(array[1], 3);
- assert.equal(array[2], 2);
- });
- });
- var arr = [ 'a', 'b', 'c', 'd', 'e'];
- var arr2 = arr.slice(0,1).concat( ['d'] ).concat( arr.slice(2,4) ).concat( arr.slice(4) );
- function magicFunction (targetArray, indexFrom, indexTo) {
- targetElement = targetArray[indexFrom];
- magicIncrement = (indexTo - indexFrom) / Math.abs (indexTo - indexFrom);
- for (Element = indexFrom; Element != indexTo; Element += magicIncrement){
- targetArray[Element] = targetArray[Element + magicIncrement];
- }
- targetArray[indexTo] = targetElement;
- }
- Array.prototype.immutableMove = function (old_index, new_index) {
- var copy = Object.assign([], this);
- if (new_index >= copy.length) {
- var k = new_index - copy.length;
- while ((k--) + 1) {
- copy.push(undefined);
- }
- }
- copy.splice(new_index, 0, copy.splice(old_index, 1)[0]);
- return copy;
- };
- Array.prototype.moveUp = function (value, by) {
- var index = this.indexOf(value),
- newPos = index - (by || 1);
- if (index === -1)
- throw new Error("Element not found in array");
- if (newPos < 0)
- newPos = 0;
- this.splice(index, 1);
- this.splice(newPos, 0, value);
- };
- Array.prototype.moveDown = function (value, by) {
- var index = this.indexOf(value),
- newPos = index + (by || 1);
- if (index === -1)
- throw new Error("Element not found in array");
- if (newPos >= this.length)
- newPos = this.length;
- this.splice(index, 1);
- this.splice(newPos, 0, value);
- };
- var arr = ['banana', 'curyWurst', 'pc', 'remembaHaruMembaru'];
- alert('withiout changes= '+arr[0]+' ||| '+arr[1]+' ||| '+arr[2]+' ||| '+arr[3]);
- arr.moveDown(arr[2]);
- alert('third word moved down= '+arr[0] + ' ||| ' + arr[1] + ' ||| ' + arr[2] + ' ||| ' + arr[3]);
- arr.moveUp(arr[2]);
- alert('third word moved up= '+arr[0] + ' ||| ' + arr[1] + ' ||| ' + arr[2] + ' ||| ' + arr[3]);
- array.move(index, howMany, toIndex);
- array = ["a", "b", "c", "d", "e", "f", "g"];
- array.move(3, 2, 1); // returns ["d","e"]
- array; // returns ["a", "d", "e", "b", "c", "f", "g"]
- Array.prototype.move || Object.defineProperty(Array.prototype, "move", {
- value: function (index, howMany, toIndex) {
- var
- array = this,
- index = parseInt(index) || 0,
- index = index < 0 ? array.length + index : index,
- toIndex = parseInt(toIndex) || 0,
- toIndex = toIndex < 0 ? array.length + toIndex : toIndex,
- toIndex = toIndex <= index ? toIndex : toIndex <= index + howMany ? index : toIndex - howMany,
- moved;
- array.splice.apply(array, [toIndex, 0].concat(moved = array.splice(index, howMany)));
- return moved;
- }
- });
- function ArrayMove(array, from, to) {
- if ( Math.abs(from - to) > 60) {
- array.splice(to, 0, array.splice(from, 1)[0]);
- } else {
- // works better when we are not moving things very far
- var target = array[from];
- var inc = (to - from) / Math.abs(to - from);
- var current = from;
- for (; current != to; current += inc) {
- array[current] = array[current + inc];
- }
- array[to] = target;
- }
- }
- Array.prototype.move = function (old_index, new_index) {
- console.log(old_index + " " + new_index);
- while (old_index < 0) {
- old_index += this.length;
- }
- while (new_index < 0) {
- new_index += this.length;
- }
- if (new_index >= this.length) {
- new_index = new_index % this.length;
- }
- this.splice(new_index, 0, this.splice(old_index, 1)[0]);
- return this; // for testing purposes
- };
- const moveItemInArrayFromIndexToIndex = (array, fromIndex, toIndex) => {
- const newArray = [...array];
- if (fromIndex === toIndex) return newArray;
- const target = newArray[fromIndex];
- const inc = toIndex < fromIndex ? -1 : 1;
- for (let i = fromIndex; i !== toIndex; i += inc) {
- newArray[i] = newArray[i + inc];
- }
- newArray[toIndex] = target;
- return newArray;
- };
Add Comment
Please, Sign In to add comment